replaceAll is not replacing the substrings

2019-08-13 02:28发布

问题:

When I run my code after I input this

1 qwertyuiopasdfghjklzxcvbnm

will

it still prints out will instead of replacing the characters like it should, why is that?

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);

    }

回答1:

Strings are immutable. Assign input to the result of replaceAll:

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

Aside: Consider using String#replace if regular expressions are not required.



回答2:

Strings are immutable in java , you cannot modify strings after creating them, consider assigning the result of replaceAll to the original string

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

The method replaceAll returns a new string after replacement.

even += doesn't add the value to the string, it creates another string internally.Thus, consider using StringBuilder for better performance if you want to modify the string very often



回答3:

I was coding this method by Friday, before leaving from job, but had to leave.

Anyhow, I did finish it today.

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();
}

How to test it? :

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));
}