【问题标题】:replaceAll is not replacing the substringsreplaceAll 不替换子字符串
【发布时间】:2013-02-16 13:25:36
【问题描述】:

当我输入这个后运行我的代码时

1 qwertyuiopasdfghjklzxcvbnm

它仍然打印出 will 而不是像它应该的那样替换字符,这是为什么呢?

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

    }

【问题讨论】:

    标签: java string replace


    【解决方案1】:

    Strings 是不可变的。将input 赋值给replaceAll 的结果:

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

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

    【讨论】:

    • 哇,我知道我为这个愚蠢的问题感到很抱歉
    • @WillJamieson 你的问题还不错,所以你不应该为此感到难过。
    • 没问题@WillJamieson,很容易犯错误。
    • +1 为什么replaceAll()?非正则表达式输入很昂贵。请改用replace()
    • 现在又遇到了另一个问题,它没有替换它应该替换的东西。
    【解决方案2】:

    字符串在java中为immutable,创建后不能修改字符串,考虑将replaceAll的结果赋给原字符串

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

    replaceAll 方法在替换后返回一个新字符串。

    即使+= 不会将值添加到字符串,它会在内部创建另一个字符串。因此,如果您想经常修改字符串,请考虑使用StringBuilder 以获得更好的性能

    【讨论】:

      【解决方案3】:

      我在周五离职前编写了这个方法,但不得不离开。

      不管怎样,我今天确实完成了。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-04-06
        • 2011-05-05
        • 1970-01-01
        • 1970-01-01
        • 2012-06-10
        • 2019-02-11
        • 1970-01-01
        相关资源
        最近更新 更多