Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.


trying to do a string find and replace

trying to do a string find and replace

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?

RE: trying to do a string find and replace

Hi Carrot,

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


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

will remove all occurrences of any whitespace.


RE: trying to do a string find and replace

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

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


RE: trying to do a string find and replace

This fixes that issue, thank you

RE: trying to do a string find and replace

Hi jc,

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


Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close