• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

remove white spaces

 
hesham katon
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
     
    Jeroen Wenting
    Ranch Hand
    Posts: 5093
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 5093
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Sheriff
    Posts: 7023
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    [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
    Posts: 56
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Sheriff
    Pie
    Posts: 20527
    54
    Chrome Eclipse IDE Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Please don't reopen a 5-year old thread. You already have your own thread for this.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic