• 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

Deleting Characters

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Would anyone know how to check for special characters (excluding some of the characters) in a String? For example: The String I am parsing is 1. MAINT & SVC*-7?8 and I want to find all the characters that I want to delete (see code below) and not have to right a huge case statement. Is there any already made java method that would check for special characters? So the sample string I want to keep the '.' and the '?'.
Keep in mind that the string could also contain many other characters that are accepted and not accepted.
Here is my code:

Thanks for your help.
Gregg
[ August 15, 2002: Message edited by: Jim Yingst ]
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, I guess it really depends what you mean by "special". The Character class has some methods you might find useful, like isLetter(), isNumber(), etc - using these may be shorter than listing all the letters/numbers/whatever. Or for a much more powerful and flexible toolset, check out the java.util.regex package. You can probably find a whole different way to approach the problem there.
If you do get stuck writing a case statement, it needn't be that bad. You've already got a fair amount of needless duplication in your code. For example if two different characters are processed the same way, you don't need to make separate case blocks for them - you can put them together, like # and & below:

Also, I'm guessing deleteCharacter is going to be a horribly inefficient method that creates a whole new String every time you want to delete a character. A smoother approach might be to create a StringBuffer. Instead of deleteing certain characters, you write your method to write all the other characters to the buffer. Then when you're done, use toString() to extract the boffer contents:

I have no idea what you're trying to do with the indexes, so I left them alone. But I suspect you're going to cause an infinite loop if you're not careful, so consider what you want to do here carefully. Good luck...
[ August 20, 2002: Message edited by: Jim Yingst ]
 
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is this what you want done?
 
Allen Thomas
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jim and John
Thank you so much for your great advice and help. I implemented both of your techniques in some capacity.
Gregg
 
reply
    Bookmark Topic Watch Topic
  • New Topic