Two Laptop Bag
The moose likes Beginning Java and the fly likes How to remove multiple spaces between words and leave 1 space between Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to remove multiple spaces between words and leave 1 space between" Watch "How to remove multiple spaces between words and leave 1 space between" New topic
Author

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

M Wilson
Ranch Hand

Joined: May 23, 2010
Posts: 41
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(???, " ");

Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 4087
    
  18

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.


The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 4087
    
  18

Here is a tutorial of regular expressions: http://download.oracle.com/javase/tutorial/essential/regex/index.html
M Wilson
Ranch Hand

Joined: May 23, 2010
Posts: 41
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
Bartender

Joined: Sep 20, 2010
Posts: 4087
    
  18

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
Bartender

Joined: Sep 20, 2010
Posts: 4087
    
  18

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

Joined: May 23, 2010
Posts: 41
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!!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to remove multiple spaces between words and leave 1 space between
 
It's not a secret anymore!