• 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

String from JTextField

 
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am getting the user input through a JTextfield.



Even though I call trim function to remove the blank spaces, I am getting this problem. When the user types like "test " (i.e name followed by space bar), the space created by space bar does not get removed from the string. How do I remove it and make it as "test". Do I have to check character by character? Thanks.
Regards
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Remember strings are immutable.
The trim() method returns a new String object from which leading and trailing whitespaces have been removed. In your code exmaple you're ignoring this return value and using the original String object - which still includes whitespaces.
 
Gopu Akraju
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes thanks. I could remove all the white spaces using simple regex as
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Gopu Akraju:
Yes thanks. I could remove all the white spaces using simple regex as

Not a good idea. That will remove included spaces and change "Gopu Akraju" to "GopuAkraju".

What you actually want is String name = nameText.getText().trim(); as Jelle Klap has already hinted.
 
Gopu Akraju
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
YEs that is true. I tried both ways and works perfect for me. I have to remove all the white spaces in the string, not only in the starting and at the end. Hence regex, gives me a compact string.
[ May 08, 2008: Message edited by: Gopu Akraju ]
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use replaceAll then, using "\\s" to remove all whitespace, not only spaces.
 
Author
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Gopu Akraju:
I could remove all the white spaces using simple regex as



Others have pointed out some problems with this, namely
  • String.replace() isn't a regex method. Use String.replaceAll() for regex replacement.
  • " " will replace only spaces, not other whitespace.
  • You probably don't want to get rid of whitespace between words.



  • But those things aside, you still have the original problem.
    name.replace(" ", "");
    does not modify name but instantiates a new String object, so you would want something like
    name = name.replace(" ", "");

    Probably easiest is to just do name = name.trim() as it doesn't have the above problems.
    [ May 08, 2008: Message edited by: Brian Cole ]
     
    Gopu Akraju
    Ranch Hand
    Posts: 242
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks and I learnt a lot.
     
    reply
      Bookmark Topic Watch Topic
    • New Topic