Removing Spaces in Strings | Sololearn: Learn to code for FREE!


Removing Spaces in Strings

I am trying to get "HelloWorld!" when I first type "Hello World!", but running this program simply returns the same "Hello World!" for me. I know using replace(" ", "") works. But why doesn't string.trim() work? It used to (on pc).

6/2/2018 5:43:05 PM

Andre Daniel

3 Answers

New Answer


Andre Daniel Ah, that's because trim is not supposed to remove spaces in the middle, only start and end. Google java+trim+whitespace


It works for me (as, your code gives the result I expect) what makes you think it is wrong?


ifl unfortunately, when I enter "Hello World", I get back "Hello World" instead of the intended "HelloWorld". How does it work for you and not me?