• 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

How to remove multiple spaces between words and leave 1 space between

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String str = "A BE LINCOLN";
str = "GEOR GE W";
There are 2 spaces between A and BE.
There are more than 2 spaces between GEOR and GE
results should be
str = "ABE LINCOLN";
str = "GEORGE W";

How do you remove spaces if spaces > 1

If I did a count for spaces, how do you use result of count to replace? Or is there an easier way to do this? Thank you very much.
int count = 0;
for (int i = 0; i < str.length(); i++)
{
if (str.charAt(i) == ' ')
{
count++;
}

}
str = str.replaceAll(???, " ");

 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Take a look at Regular Expressions. They are described in the Pattern class documentation.

Use str.replaceAll(regex, "") with regex being the regular expression. Hint: Look closely at the greedy quantifiers.
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is a tutorial of regular expressions: http://download.oracle.com/javase/tutorial/essential/regex/index.html
 
M Wilson
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is what I tried and it works as expect!
Thank you very much


String str = "AB E LINCOLN"; //where there are 2 or more spaces between
String regex = "\\s{2,}";
str = str.replaceAll(regex, " ");
result in str = ABE LINCOLN
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nice job, glad you worked it out. Note that \s will replace *any* white space character, including line feeds and tabs. Your solution is excellent, but keep in mind that if you want to filter *only* spaces, you can use " {2,}".
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now that I look at it more closely, your program shouldn't work, for the simple reason that you replace multiple spaces with a single space, instead of an empty string.

The outcome should be "AB E LINCOLN" (with a single space between AB and E that is).
 
M Wilson
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are correct, it there is only one space it should do nothing, if more than one space it should replace with 1 space.
For some reason I posted str = "AB{space}{space}{space}E" it show there was only one space but actually there were about 3 spaces between. I ran the code sample and it does work. Thanks much!!!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic