Solved

Trimming a String

Posted on 2004-04-21
10
401 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 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
Technology Partners: 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

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

691 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