Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

trying to do a string find and replace 1

Status
Not open for further replies.

jcarrott

Programmer
May 28, 2009
130
US
I have a string that sometimes contains spaces and the number of spaces varies. I know how to do this in Perl, but not in Java.

An example of the data is;

800,563751,L6441608-013 000039
120,563751,L557160-166 000044

There are only 3 values; company, vendor, invoice

I need to remove the spaces.

Is there an easy way to do this?
 
Hi Carrot,

You should be able to use the replaceAll() method of the string. I am assuming they are immutable strings BTW.

i.e.

newString = oldString.replaceAll("\s+", "");

will remove all occurrences of any whitespace.

Cheers,
Scott
 
ah.. just re-read my answer. You need to escape the \s+, so the line should actually read:

newString = oldString.replaceAll("\\s+", "");

Cheers,
Scott
 
Hi jc,

Since you have a Perl background, you will want to check out Java support for regular expressions: java.util.regex

Smiles,
Jim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top