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


     给定三个字符串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));        
        }
    
    }
  • 相关阅读:
    nyoj 95 众数问题(set)
    nyoj 93 汉诺塔(三)(stack)
    hdu 1010 Tempter of the Bone
    nyoj 55 懒省事的小明(priority_queue优先队列)
    nyoj 31 5个数求最值
    poj 1256 Anagram
    next_permutation函数
    nyoj 19 擅长排列的小明(深搜,next_permutation)
    nyoj 8 一种排序(用vector,sort,不用set)
    nyoj 5 Binary String Matching(string)
  • 原文地址:https://www.cnblogs.com/toov5/p/7396412.html
Copyright © 2020-2023  润新知