Java replace() problems

后端 未结 2 1208
滥情空心
滥情空心 2021-01-22 10:12

I\'m supposed to input a string, and replace all and, to, you, and for substrings with &, 2,

2条回答
  •  忘掉有多难
    2021-01-22 11:03

    Here is how I simplified your code and got the correct result:

        String rope = "and , and,and , to , to,to , you ,you , you, for ,for , for,a , a,e , e,i , i,o , o,u , u";
    
       // rope = rope.replaceAll(" ", "");
        rope = rope.replaceAll("and", "&");
        rope = rope.replaceAll("to", "2");
        rope = rope.replaceAll("you", "U");
        rope = rope.replaceAll("for", "4");
        rope = rope.replaceAll("a", "");
        rope = rope.replaceAll("e", "");
        rope = rope.replaceAll("i", "");
        rope = rope.replaceAll("o", "");
        rope = rope.replaceAll("u", "");
        System.out.println(rope);
    

提交回复
热议问题