如何在字符串Java中使用3个或更多字符查找和更换3个或更多个字符



我需要检查该行是否包含必须是的字符串消除并指出将消除哪些符号。
如果有三个或更多连续的字符具有相同的符号,则将字符序列替换为序列长度(" ")。例如,行" ,_, @, @, @, @,$,$,$,,#,#,#,!"将转换为" ,_,_,_,_,_,_,_,$,$,$,_,_,#,#,!"消除过程后。
我只需要使用字符串或StringBuilder,REGEX,ECT ...(仅Java的基本编码)。
也不能使用数组。预先感谢。
这就是我尝试的:

public static void main(String[] args) {    
    String linha = "##,$$$$,%%%%,@%@@@,!!!!", validos = "$#%!@";        
        for (int i = 0; i < validos.length(); i++) {
            linha = linha.replaceAll("\" + validos.charAt(i) + "{3,}", "_");
        }
        System.out.println (linha);
    }
}

这里的问题在于,仅替换一个" _"序列,我不知道哪些字符被替换。

肯定可以在许多方面做到这一点,这可能是一个很好的练习。在这里,您只需使用基本的循环结构就可以实现基本的实现,而没有像Stringutils库这样的花哨的实现...请注意,您以前的循环实现会错过在linha不同位置重复相同角色的几次出现。

static int index(String lookInStr, char lookUpChr) {
    return lookInStr.indexOf(new String(new char[] { lookUpChr, lookUpChr, lookUpChr }));
}
public static void main(String[] args) {
    String linha = "####,@@@@@@@@,$$$$,%%%%,@%@@@,!!!!", validos = "$#%!@";
    for (int i = 0; i < validos.length(); i++) {
        char currentSearchChar = validos.charAt(i);
        do {
            int index = index(linha, currentSearchChar);
            if (index >= 0) {
                int count = -1;
                do {
                    count++;
                } while (linha.charAt(count + index) == currentSearchChar && count + index < linha.length() - 1);
                String replacementSeq = "";
                for (int j = 0; j < count; j++) {
                    replacementSeq += "-";
                }
                linha = linha.replaceAll("\" + validos.charAt(i) + "{" + count + ",}", replacementSeq);
            }
        } while (index(linha, currentSearchChar) >= 0);
    }
    System.out.println(linha);
}

如果您尝试一次替换三个字符,而您想要三个下划线,那么您只是缺少这一点:

linha = linha.replaceAll("\" + validos.charAt(i) + "{3,}", "___");

如果您希望它们被逗号分隔:

linha = linha.replaceAll("\" + validos.charAt(i) + "{3,}", "_,_,_");

基本上,这将字符串拆分为单独的块,然后检查块的长度并返回原始块,或者用下划线代替。

static String convert(String s) {
    StringBuilder sb = new StringBuilder();
    for(int i = 0; i < s.length(); i++) {
        char c = s.charAt(i);
        StringBuilder tempSb = new StringBuilder();
        for(; i < s.length(); i++) {
            char d = s.charAt(i);
            if(d != c) {
                i--;
                break;
            } else {
                tempSb.append(d);
            }
        }
        String t = tempSb.toString();
        if(t.length() < 3) {
            sb.append(t);
        } else {
            sb.append(repeat("_", t.length()));
        }
    }
    return sb.toString();
}
public static void main(String[] args) {
    String x = convert("##,$$$$,%%%%,@%@@@,!!!!");
    System.out.println(x); // ##,____,____,@%___,____
}

这是简单的重复方法:

static String repeat(String s, int repeatCount) {
    StringBuilder sb = new StringBuilder();
    for(int i = 0; i < repeatCount; i++) {
        sb.append(s);
    }
    return sb.toString();
}

尚未真正实现此功能,但这是您可能会看到的:

在Matcher中,有find(int start)start()end()

有一个" 3或重复性char"的模式(您可以在问题中参考评论)。

psuedo代码是这样的:

int lastEndingPosition = 0;
StringBuilder sb;
while (matcher can find next group) {
  // add the unmatched part
  sb.append( substring of input string from lastEndingPosition to matcher.start() ); 
  // add the matched part
  sb.append( "-" for matcher.end() - matcher.start() times);
  lastEndingPosition = matcher.end();
}
sb.append( substring of input string from lastEndingPosition to the end);

可能有一些更优雅的方法可以做到这一点。这只是一种替代

最新更新