1

Possible Duplicate:
JAVA: check a string if there is a special character in it

I'm testing a method that transforms a string into another string except it preservers all special characters (non alpha-numeric).

So I'm looking to test the output of this method to ensure it actually preserves these characters.
I know this calls for use of Pattern and Matcher classes but not sure how.
I think I need to build a format template, compile it with Pattern and then use it with Matcher with the output string of my test method .
So I'll build the format template character by character. For digits and characters, I can use IsLetter and IsDigit of Character class and insert "\\d" for digit in my template.
Not sure what I should use for letters and special characters.

Any Ideas?

Thanks.

Community
  • 1
  • 1
Ankur
  • 11,239
  • 22
  • 63
  • 66
  • Define "preserves" - does that mean `"abc!d." --> " ! ."` or `"abc!d." --> "!."` or something else? Please give sample input and output data – Bohemian Sep 13 '11 at 07:13

1 Answers1

0

You should write pattern that replaces all forbidden characters to nothing and use String.replaceAll() method. Something like the following:

str = str.replaceAll("[^\\w_\\d]", "");

AlexR
  • 114,158
  • 16
  • 130
  • 208