I am trying to replace a space character into a hyphen I have in my string.
String replaceText = \"AT AT\";
replaceText.replace(\' \', \'-\');
You need to save the value returned by replace()
. If you want to replace more than one occurrence, use replaceAll()
.
String replaceText = "AT AT";
replaceText = replaceText.replaceAll(" ", "-");
As @Mark Peters points out in the comments, replace(Char, Char)
is sufficient (and faster) for replacing all occurrences.
String replaceText = "AT AT";
replaceText = replaceText.replace(' ', '-');
In case this fact bothers you: immutability is a Good Thing.