2013-03-01 73 views
1

当我后,我输入我的代码运行这个的replaceAll没有更换子

1 qwertyuiopasdfghjklzxcvbnm

它仍然打印出而不是更换喜欢的人物它应该,为什么呢?

public static void main(String[] args) 
{ 

    String input=""; 
    int cases= sc.nextInt(); 
    String al= sc.next(); 
    String upAl= al.toUpperCase(); 
    char [] upAlph = upAl.toCharArray(); 


    char[] alph = al.toCharArray(); 
    for(int i=0; i<cases; i++) 
    { 
     input=sc.next(); 

     input.replaceAll("a", ""+ alph[0]); 
     input.replaceAll("b", ""+ alph[1]); 
     input.replaceAll("c", ""+ alph[2]); 
     input.replaceAll("d", ""+ alph[3]); 
     input.replaceAll("e", ""+ alph[4]); 
     input.replaceAll("f", ""+ alph[5]); 
     input.replaceAll("g", ""+ alph[6]); 
     input.replaceAll("h", ""+ alph[7]); 
     input.replaceAll("i", ""+ alph[8]); 
     input.replaceAll("j", ""+ alph[9]); 
     input.replaceAll("k", ""+ alph[10]); 
     input.replaceAll("l", ""+ alph[11]); 
     input.replaceAll("m", ""+ alph[12]); 
     input.replaceAll("n", ""+ alph[13]); 
     input.replaceAll("o", ""+ alph[14]); 
     input.replaceAll("p", ""+ alph[15]); 
     input.replaceAll("q", ""+ alph[16]); 
     input.replaceAll("r", ""+ alph[17]); 
     input.replaceAll("s", ""+ alph[18]); 
     input.replaceAll("t", ""+ alph[19]); 
     input.replaceAll("u", ""+ alph[20]); 
     input.replaceAll("v", ""+ alph[21]); 
     input.replaceAll("w", ""+ alph[22]); 
     input.replaceAll("x", ""+ alph[23]); 
     input.replaceAll("y", ""+ alph[24]); 
     input.replaceAll("z", ""+ alph[25]); 
     input.replaceAll("A", upAlph[0]+""); 
     input.replaceAll("B", upAlph[1]+""); 
     input.replaceAll("C", upAlph[2]+""); 
     input.replaceAll("D", upAlph[3]+""); 
     input.replaceAll("E", upAlph[4]+""); 
     input.replaceAll("F", upAlph[5]+""); 
     input.replaceAll("G", upAlph[6]+""); 
     input.replaceAll("H", upAlph[7]+""); 
     input.replaceAll("I", upAlph[8]+""); 
     input.replaceAll("J", upAlph[9]+""); 
     input.replaceAll("K", upAlph[10]+""); 
     input.replaceAll("L", upAlph[11]+""); 
     input.replaceAll("M", upAlph[12]+""); 
     input.replaceAll("N", upAlph[13]+""); 
     input.replaceAll("O", upAlph[14]+""); 
     input.replaceAll("P", upAlph[15]+""); 
     input.replaceAll("Q", upAlph[16]+""); 
     input.replaceAll("R", upAlph[17]+""); 
     input.replaceAll("S", upAlph[18]+""); 
     input.replaceAll("T", upAlph[19]+""); 
     input.replaceAll("U", upAlph[20]+""); 
     input.replaceAll("V", upAlph[21]+""); 
     input.replaceAll("W", upAlph[22]+""); 
     input.replaceAll("X", upAlph[23]+""); 
     input.replaceAll("Y", upAlph[24]+""); 
     input.replaceAll("Z", upAlph[25]+""); 
     input.replaceAll("_", " "); 
     pw.println(input); 

    } 

回答

8

Strings是不可变的。分配inputreplaceAll结果:

input = input.replaceAll("a", ""+ alph[0]); 
... 

旁白:如果不需要正则表达式考虑使用String#replace

+1

哇我知道,我觉得自己像一个白痴的愚蠢的问题 – 2013-03-01 22:38:16

+0

对不起@WillJamieson你的问题是没有那么糟糕,所以你不应该感到很内疚。 – 2013-03-01 22:38:49

+0

没问题@WillJamieson,一个容易犯的错误。 – Reimeus 2013-03-01 22:39:48

2

字符串在Java immutable,你不能修改字符串创建它们后,考虑分配的replaceAll结果到原始字符串

input = input.replaceAll("string", "string"); 

replaceAll返回后更换一个新的字符串的方法。

即使+=值不添加到字符串,它创建如果要修改字符串经常

+0

+1使用'替换()'而不是'replaceAll()'。 – 2013-03-01 22:43:27

1

我在周五编码这种方法的另一个字符串internally.Thus,可以考虑使用StringBuilder获得更好的性能,离开工作之前,但不得不离开。无论如何,我今天完成了它。

public static String replaceStringChars(String input, String abc, String replacement, boolean ignoreCase) { 
    abc = (ignoreCase ? abc.toLowerCase() : abc) + (ignoreCase ? abc.toUpperCase() : "") + "_"; 
    replacement = (ignoreCase ? replacement.toLowerCase() : replacement) + (ignoreCase ? replacement.toUpperCase() : "") + " "; 
    input = input.replace(abc, replacement); 
    StringBuilder newString = new StringBuilder(input); 
    for (int a = 0; a < input.length(); a++) { 
     char chr = input.charAt(a); 
     int index = abc.indexOf(chr); 
     if (index >= 0) { 
      newString.setCharAt(a, replacement.charAt(index)); 
     } 
    } 
    return newString.toString(); 
} 

如何测试? :

public static void main(String [] args) 
{ 
    String input = "will_is_my_pastor"; 
    String abc = "abcdefghijklmnopqrstuvwxyz"; 
    String replacement = "qwertyuiopasdfghjklzxcvbnm"; 
    System.out.println(abc); 
    System.out.println(replacement); 
    System.out.println(replaceStringChars(input, abc, replacement, true)); 
}