Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Trimming a String

Posted on 2004-04-21
10
Medium Priority
?
407 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 200 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

636 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