ben ben - 2 years ago 119
Java Question

Replace Space to Hyphen

I am trying to replace a space character into a hyphen I have in my string.

String replaceText = "AT AT";
replaceText.replace(' ', '-');

but when I do this, I cannot seem to replace the character. I tried the
method and it doesn't work either.


sorry my mistake.. the result of late night programming :(

thanks for the answer i cant probably answer all so i will check the first answer

replaceText = replaceText.replace(' ', '-');

Answer Source
replaceText = replaceText.replace(' ', '-');

Strings are immutable, they cannot be changed after creation. All methods that somehow modify a string will return a new string with the modifications incorporated.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download