Currently, I have an array of Strings, each with random characters in it that are random in length. I want to replace every “A” with an “X”, how would I come about doing this?
Example:
String str = "ABCDEFGAZYXW";
I want the String to become "XBCDEFGXZYXW". I tried to use:
str.replaceAll("A", "X");
But it does not change the string. Any help is greatly appreciated!
str = str.replaceAll("A", "X");The
replaceAllmethod doesn’t change the string (strings in Java are immutable) but creates a newStringobject and returns it as a result of the function call. In this way we change the reference to the new object where the old one is not changed but simply not referenced.