• Post Reply Bookmark Topic Watch Topic
  • New Topic

String replace() method  RSS feed

 
Cyrus Serrano
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can i remove spaces on a String?

For example myString = "Java is Cool";

this should return "JavaisCool"..

the replace method of the String class cant seem to work,
i did :

myString.replace('\u0020','\u0000');

'\u0020' is a space
'\u0000' is null

Any help?

thanks
 
Sanjaya Sugiarto
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try:
myString.replace(" ", "");
 
Alan Moore
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The method replace(CharSequence, CharSequence) was added in JDK 1.5; that's what Sanjaya is talking about. Before that, there was only the one "replace" method, which (as you've seen) doesn't do what you want. If you're running JDK1.4.x, you can use replaceAll(" ", "") instead. Also, remember that strings are immutable, so you have to take the return value of whichever method you use:
 
Cyrus Serrano
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks i'll try this out.
One more thing should i just use StringBuffer?

 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
remember that strings are immutable. so if you call a method that "changes" the string, you don't actually change the string. you generate a NEW string with the changes. if you don't then save that new string, it gets lost.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another remark with regard to your original idea: Replacing a character by a null character does not remove the original character from the string. It just replaces the character with another character with code 0.

And I'll repeat what Fred and Alan said, because it's a very important notion: String is immutable, which means that you cannot modify a String after creating it. Class String does not contain any methods that modify the String object itself. Instead, the methods of class String return a new String object. So replace(...) does not change the String itself, it returns a new String object that contains the modified value. So you must write this:

// Note! Assign the return value of replaceAll to the variable str
str = str.replaceAll(" ", "");

Just doing this will not work:

// Doesn't work because String is immutable
str.replaceAll(" ", "");
 
Ashutosh Singh Jadon
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alan Moore wrote:The method replace(CharSequence, CharSequence) was added in JDK 1.5; that's what Sanjaya is talking about. Before that, there was only the one "replace" method, which (as you've seen) doesn't do what you want. If you're running JDK1.4.x, you can use replaceAll(" ", "") instead. Also, remember that strings are immutable, so you have to take the return value of whichever method you use:

hi Alan,
can i use the same method for replacing all $ with '''' . like replaceAll("$",'''') . if not then how can we replace a $ sign with space in java1.4 ?
 
Rob Spoor
Sheriff
Posts: 21095
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Replacing a $ with a space is simple: str.replace('$', ' '). That's because both $ and a space are single characters.
Replacing a $ with empty Strings is a bit harder, because $ has special meaning in regular expressions (which is what replaceAll takes). You need to escape that in one of two ways:
1) str.replace("\\$", ""). To escape any regular expression meta character, you need to add two backslashes in front of it - one to escape the meta character, and one to escape the \ so you can use it inside Strings.
2) str.replace("\\Q$\\E", ""). Anything between \Q and \E is treated as literal values in regular expression. There is method Pattern.quote to make this easier, but unfortunately that method is available since Java 5.0...

Speaking of Java 1.4 - why are you still using that fossil? It's been EOL (end of life) for several years already.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!