This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
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


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
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: 32
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: 3615
    
  14

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
Bartender

Joined: Sep 20, 2010
Posts: 3615
    
  14

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: 32
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: 3615
    
  14

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: 3615
    
  14

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: 32
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