Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

remove white spaces

 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
     
    Ranch Hand
    Posts: 5093
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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...]
     
    Ranch Hand
    Posts: 56
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
     
    Sheriff
    Posts: 22791
    131
    Eclipse IDE Spring Chrome Java Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Please don't reopen a 5-year old thread. You already have your own thread for this.
     
    Slideshow boring ... losing consciousness ... just gonna take a quick nap on this tiny ad ...
    We need your help - Coderanch server fundraiser
    https://coderanch.com/t/782867/Coderanch-server-fundraiser
    reply
      Bookmark Topic Watch Topic
    • New Topic