• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String replace() method

 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try:
myString.replace(" ", "");
 
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks i'll try this out.
One more thing should i just use StringBuffer?

 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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(" ", "");
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic