Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 replaceAll() method and it doesn't work either.

++++++Answer+++++++

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(' ', '-');
like image 841
ben Avatar asked Mar 10 '11 16:03

ben


People also ask

How do you replace a hyphen in space?

Use the replaceAll() method to replace spaces with dashes in a string, e.g. str. replaceAll(' ', '-') . The replaceAll method will return a new string where all spaces are replaced by dashes. Copied!

How do you replace a space with a dash in Python?

To replace a space with a dash in Python, the easiest way is to use the Python built-in string replace() function. string_with_spaces = "This is a string." string_with_dashes = string_with_spaces. replace(" ","-") print(string_with_dashes) #Output: This-is-a-string.


1 Answers

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.

like image 112
Erik Avatar answered Oct 13 '22 01:10

Erik