wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes String - remove more than one space between words  Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String - remove more than one space between words  " Watch "String - remove more than one space between words  " New topic
Author

String - remove more than one space between words

kri shan
Ranch Hand

Joined: Apr 08, 2004
Posts: 1372
How can i remove more than one space between words ?
example My String k = " hai how are you". Wherever my String has more than one space, it should convert/reduce to one space between words. Here between (hai and how) and (are and you) has more than one space.
Mark Van Tuyl
Ranch Hand

Joined: Mar 22, 2002
Posts: 60
This will work. It may not be the best option, though.


<a href="http://www.catb.org/~esr/faqs/smart-questions.html" target="_blank" rel="nofollow">How To Ask Smart Questions</a>
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
With

text = text.replaceAll(" +", " ");

you won't need the loop any longer. "+" means "one or more of the preceeding character.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
You can make it even more generic like this:

text = text.replaceAll("\\s+", " ");

which will replace multiple occurences of any whitespace char with a single space char.

Layne


Java API Documentation
The Java Tutorial
kri shan
Ranch Hand

Joined: Apr 08, 2004
Posts: 1372
i am using jdk 1.3. Jdk 1.3 doesn't support replaceAll(String,String)
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Assuming you can't upgrade the version you are using, then you might like to look at a third party library like Jaklarta Regexp.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
kri shan
Ranch Hand

Joined: Apr 08, 2004
Posts: 1372
I browsed Jakarta Commons RegExp.How Jakarta RegExp helps ?
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Its a replacement for the regular expression support missing in pre-1.4 JDKs. You can use it to find the position of a pattern in a string. Once you know the position, you can replace it.
kri shan
Ranch Hand

Joined: Apr 08, 2004
Posts: 1372
I need one sample code..
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Show us what you have tried and maybe we can help.
kri shan
Ranch Hand

Joined: Apr 08, 2004
Posts: 1372
I am using jdk 1.3 How can i remove more than one space between words ?
example My String k = " hai how are you". Wherever my String has more than one space, it should convert/reduce to one space between words. Here between (hai and how) and (are and you) has more than one space.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Take a look at the JavaDoc for replaceAll in JDK1.4. There is an example for alternative code that should give you a good idea of how to use JakartaRegex to achieve the same.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: String - remove more than one space between words
 
Similar Threads
Making spaces in between words
String handling
jakarta commons Lang's StringUtil
String handling
Regular expression to take integers out of a string