Trimming a String

Hi

Is there a way of getting rid of spaces in a string which are larger than one i.e

"L-1011 TriStar.>tri4pak.zip >FS2004 Lockheed         L-1011 TriStar."

would become "L-1011 TriStar.>tri4pak.zip >FS2004 Lockheed L-1011 TriStar."

Thanks

Akbar
akzahAsked:
Who is Participating?
 
Mayank SConnect With a Mentor Associate Director - Product EngineeringCommented:
Oops. Sorry, same time ;-)

Perhaps this does exactly what you want - http://www.javaalmanac.com/egs/java.util.regex/RemDupSpace.html
0
 
CEHJCommented:
s = s.replaceAll("\\s+", "");
0
 
Mayank SAssociate Director - Product EngineeringCommented:
You can use the replaceAll () method.
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
Mayank SAssociate Director - Product EngineeringCommented:
0
 
CEHJCommented:
Sorry - slightly misunderstod that. Try this:

s = s.replaceAll("(\\s)\\s*?", "$1");
0
 
Nick_72Commented:
why not just  
s = s.replaceAll("  ", "");  //two spaces in the first argument
0
 
akzahAuthor Commented:
Hi

mayankeagle the remdup space page, was correct, thanks very much guys. Cehj both of your solutions got rid of all the white spaces.

Akbar
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Then please close the question.
0
 
CEHJCommented:
Sorry (untested). This IS tested and works:

s = s.replaceAll("\\s{2,}?", "");
0
 
CEHJCommented:
...and this also works:

s = s.replaceAll("\\s+", " ");
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.