• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Eliminate unnecessary characters from a String

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a method which accepts a String and after eliminating some unnecessary characters like @,* etc it returns the String.

I have not been able to totally eliminate the not allowed characters, but have managed to replace them with a $ sign.
Can someone please tell me how to eliminate the uneeded characters.


[ July 29, 2005: Message edited by: Smita Chopra ]
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Perhaps the String.replaceAll() method is what you want. It takes a regular expression and replaces all occurrances of the regular expression with the substitute value.



This replaces all occuranecs of @, %, ., and * with the empty string, thereby eliminating them from your string.

Your way could work, albeit much less efficiently, with simply elimating the characters instead of replacing them with a dollar sign, akin to:

 
Smita Chopra
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Joel McNary:
Perhaps the String.replaceAll() method is what you want. It takes a regular expression and replaces all occurrances of the regular expression with the substitute value.



This replaces all occuranecs of @, %, ., and * with the empty string, thereby eliminating them from your string.

Your way could work, albeit much less efficiently, with simply elimating the characters instead of replacing them with a dollar sign, akin to:



Thanks a lot. It was much easier than I thought
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As mentioned the Java API contains a lot of helpful tools to simplify common tasks like this. However, as a learning exercise, sometimes it's good to implement it yourself as you tried in your original approach. If you are interested in fixing your original code, you may want to look at the StringBuffer or StringBuilder (in Java 1.5) classes. Since String is immutable, these classes allow you to dynamically build a String from other Strings or primitives.

HTH

Layne
 
Good heavens! What have you done! Here, try to fix it with this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic