Solved

Trimming a String

Posted on 2004-04-21
10
381 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupNoAdj 7 96
groupSum5 challenge 5 85
Java Loop 6 62
Error in @AspectJ Based AOP with Spring 2 13
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

809 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