字符串问题之 替换字符串中连续出现的指定字符串

 给定三个字符串str from 和to,from中无重复字符串,把str中所有from的子串全部替换成to字符串,对连续出现from的部分要求只替换成一个to字符串,返回最终的结果字符串。

str = ”123abc“ from=”abc“ to="4567"  返回”1234567“

解决本题的思路

  

   把str看作数组,

   把str中的from位置字码设为0(空字符)

  1、生成整形变量match,表示目前匹配到from字符串的位置,初始时match=0;

  2、从左到右遍历str中的每个字符,str[i]

  3 、 str[i]==from[match] . 若match是最后一个字符的位置,则把从i向左from.length()个位置字符编码设置0,若不是最后一个,match++。继续遍历

  4      str[i]!==from[match] 匹配失败, match=0,从新开始匹配。如果str[i]==from[0] 从当前字符开始从新匹配过程,否则继续遍历下一个字符

最后拼接起来

上代码:

package TT;



public class Test3 {
 
    public static String replace(String str, String from, String to){
        if(str==null || from ==null || str.equals(" ")|| from.equals(" ")){
            return str;
        }
        
        char[] chas = str.toCharArray();
        char[] chaf = from.toCharArray();
        
        int match = 0;
        
        for(int i =0; i<chas.length; i++){
              if(chas[i]==chaf[match++]){
                   if(match==chaf.length){
                        clear(chas, i, chaf.length);
                        match = 0;
                   }
                 
             }else{
                 if(chas[i]==chaf[0]){
                     i--;
                 }
                 match = 0;
             }
        }
        
        String res = "";
        String cur = "";
        for(int i =0; i<chas.length; i++){
            if(chas[i]!=0){
                cur = cur+String.valueOf(chas[i]);
            }
            if(chas[i]==0 && (i==0 || chas[i-1]!=0)){
                res = res+cur+to;
                cur="";
            }
        }
        
        if(!cur.equals(" ")){
            res = res+cur;
        }
        return res;
    }
    
    public static void clear(char[] chas, int end, int len){
        while(len-- !=0){
             chas[end--]=0;
        }
    }
    
    public static void main(String[] args){
        String s = "123abc";
        String from ="abc";
        String to = "456";
        
        String ss = replace(s,from,to);
        
        System.out.println(ss);
    }
    
}

结果:

 还有这种作弊的方法:哈哈哈

public static void main(String[] args) {
        String str = "abcderfdsets234sdfsdf";
        String from = "234";
        String to = "bbb";
        System.out.println(str.replaceAll(from,to)); 
        
    }

或者这样:

 但是需要对于连续出现的,统计出来。替换成一个 to

public class Test6 {
    
    public static String getReplace(String str,String from,String to) {
        
        int indexOf = str.indexOf(from);
        if (indexOf != -1) {
            if (indexOf == 0) {
            String str1 = to.concat(str.substring(indexOf));
            return  getReplace(str1, from, to);
            }else {
               String str2 = str.substring(0, indexOf).concat(to).concat(str.substring(indexOf));
               return getReplace(str2, from, to);
            }    
        }else {
            return str;
        }    
    }
    
    public static void main(String[] args) {
        String str = "abcderfdsets234sdf234sdf234";
        String from = "234";
        String to = "bbb";
        System.out.println(str.replace(from, to));        
    }

}
原文地址:https://www.cnblogs.com/toov5/p/7396412.html