Solved

Trimming a String

Posted on 2004-04-21
10
375 Views
Last Modified: 2008-02-01
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
Comment
Question by:akzah
10 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 10879187
s = s.replaceAll("\\s+", "");
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10879200
You can use the replaceAll () method.
0
 
LVL 30

Accepted Solution

by:
Mayank S earned 50 total points
ID: 10879213
Oops. Sorry, same time ;-)

Perhaps this does exactly what you want - http://www.javaalmanac.com/egs/java.util.regex/RemDupSpace.html
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10879220
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10879227
Sorry - slightly misunderstod that. Try this:

s = s.replaceAll("(\\s)\\s*?", "$1");
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 9

Expert Comment

by:Nick_72
ID: 10879367
why not just  
s = s.replaceAll("  ", "");  //two spaces in the first argument
0
 

Author Comment

by:akzah
ID: 10879375
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 10879401
Then please close the question.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10879410
Sorry (untested). This IS tested and works:

s = s.replaceAll("\\s{2,}?", "");
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10879463
...and this also works:

s = s.replaceAll("\\s+", " ");
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
network + 7 80
mockito example issue 8 64
java.io.BufferedReader   readLine()  //returns the entire file ? 4 60
egit plugin on eclipse 8 40
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now