1

can you help me with this code:

 s=br.readLine(); 
 s.replace("ch", "c");  
 s.replace("ch", "c");
 s.replace("sh", "s");
 s.replace("zh", "z"); 
 s.replace("Ch", "C");


s.replace("Sh", "S");
s.replace("Zh", "Z"); 
System.out.println(s);

The problem is that the replace method doesn't work, It doesn't replace the ch,sh,zh chars in the String s, so on the output i still get the same string without change.

picaCHuXX
  • 89
  • 6

1 Answers1

5

String is immutable. replace returns a new String, which must be assigned back to s in order for s to refer (in the end) to the output String after all the replacements :

 s=br.readLine(); 
 s=s.replace("ch", "c");  
 s=s.replace("ch", "c");
 s=s.replace("sh", "s");
 s=s.replace("zh", "z"); 
 s=s.replace("Ch", "C");
 s=s.replace("Sh", "S");
 s=s.replace("Zh", "Z"); 
 System.out.println(s);
Eran
  • 374,785
  • 51
  • 663
  • 734