• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 408
  • Last Modified:

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
0
akzah
Asked:
akzah
1 Solution
 
CEHJCommented:
s = s.replaceAll("\\s+", "");
0
 
Mayank SAssociate Director - Product EngineeringCommented:
You can use the replaceAll () method.
0
 
Mayank SAssociate 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now