I am trying to replace a +
character into a hyphen
I have in my string.
String str = \"word+word\";
str.replaceAll(\'+ \', \'-\');
The replaceAll
function takes a regular expression as its first argument. It so happens that +
is a special character in regular expression language. Try replacing +
with \\+
. This will escape the plus sign, thus making the code to treat it like a normal character.
Also, the replaceAll
method yields a string, so that will not work. Try doing:
String str = "word+word";
str = str.replaceAll("\\+ ", "-");