I want to replace a character in string with the replace all method, but this method gives me still the same string.
String example = "5x";
example.replaceAll(Character.toString('x') , Integer.toString(1));
What is wrong with the code?
I want to replace a character in string with the replace all method, but this method gives me still the same string.
String example = "5x";
example.replaceAll(Character.toString('x') , Integer.toString(1));
What is wrong with the code?
String
is immutable. You should do something like
example = example.replaceAll(Character.toString('x') , Integer.toString(1));
Strings are immutable, meaning they can not be changed.
This can be done simply like this:
String example = "5x";
example = example.replaceAll("x", Integer.toString(1));
You are missing assigning the new string to example.