File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes remove white spaces Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "remove white spaces" Watch "remove white spaces" New topic

remove white spaces

hesham katon
Ranch Hand

Joined: Dec 18, 2002
Posts: 82
Hello All,
I want to remove white spaces from a string
i know that trim() is used to remove white spaces but from both ends of the string
but how if those white spaces inside the String .
i wish anyone can help
thanks in advancd

Hesham Katon<br />SCJP 1.2<br />SCWCD 1.4<br />Better to light a candle than to curse the darkness
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
You could probably write a routine that makes use of a regex without too much trouble.
If you haven't started learning about the java.util.regex package, then this might be a darn good time to do so.
  • An Introduction to java.util.regex - Lesson 1
  • An Introduction to java.util.regex - Lesson 2

  • [ February 04, 2004: Message edited by: Dirk Schreckmann ]

    [How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
    Jeroen Wenting
    Ranch Hand

    Joined: Oct 12, 2000
    Posts: 5093
    Several ways, but no real one-shot solution.
    1) use a regular expression to filter the string, returning everything but whitespace
    2) go through the string character by character, building a new string based on the old one minus the spaces
    3) use a StringTokenizer, tokenizing on spaces, and create a new string from the resulting chunks.
    I guess the first is fastest to execute, the 2nd faster to code (unless you're a RegExp guru).

    Jeroen Wenting
    Ranch Hand

    Joined: Oct 12, 2000
    Posts: 5093
    Dirk, any timeframe on parts 3 and 4 of your series on Regular Expressions?
    Makes for interesting reading for those who haven't been bitten by them a lot (like me) coming from places that are not Perl
    Dirk Schreckmann

    Joined: Dec 10, 2001
    Posts: 7023
    [Continuing a light thread hijack...]
    It's been almost a year-and-a-half since I wrote those four articles, of which only the first two have made an appearance. I still feel stuck on the first two as the feedback I've received from beginners was simply of the nature "This is hard!" which isn't what I was striving for. (Developers with previous regex experience have seemed to appreciate them, so far.)
    So, I've been meaning to spend some time figuring out improving them for the beginner to regular expressions.
    [/Continuing a light thread hijack...]
    pavithra murthy
    Ranch Hand

    Joined: Feb 06, 2009
    Posts: 56
    hi hesham ,

    were you successful in the removing the spaces between two strings .
    however trim is used to remove leading and trailing white spaces .
    Could you please provide me the inputs to do this .

    i have posted a topic in java(intermediate) for this .

    thanks in advance

    pavithra murthy
    Rob Spoor

    Joined: Oct 27, 2005
    Posts: 20269

    Please don't reopen a 5-year old thread. You already have your own thread for this.

    SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
    How To Ask Questions How To Answer Questions
    I agree. Here's the link:
    subject: remove white spaces
    jQuery in Action, 3rd edition