Solved

Trimming a String

Posted on 2004-04-21
10
361 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:mayankeagle
ID: 10879200
You can use the replaceAll () method.
0
 
LVL 30

Accepted Solution

by:
mayankeagle 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:mayankeagle
ID: 10879220
0
 
LVL 86

Expert Comment

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

s = s.replaceAll("(\\s)\\s*?", "$1");
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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:mayankeagle
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 your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
array6 challenfge 6 62
Problem to start Neon 20 51
recursion example 16 68
Java DateChooser? 2 5
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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 arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
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:

708 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

14 Experts available now in Live!

Get 1:1 Help Now