Strings are immutable - mostly for safety/security reasons that are beyond me. Once made their contents are not altered. The replace() method is returning the altered string, but it needs a new home.
String s = "test"; String x = s.replace("s", "x"); System.out.println(s); System.out.println(x);
This turns test to text, but you won't see the result in s, only in x.
posted 9 years ago
You say in your question that you want to chage test's to test\'s, but your code is trying to change it to test'/s
Assuming you wanted what you said in the question, you need to escape the '\', because '\' is a special character in Java Strings. Try
@Ronda - this will work because test is a String reference, so the second line actually just points it at the new String and the original String is no longer referenced and so is available for garbage collection.
What could go wrong in a swell place like "The Evil Eye"? Or with this tiny ad?