Check if a String contains a special character

blue picture blue · Nov 25, 2009 · Viewed 354.2k times · Source

How do you check if a String contains a special character like:

[,],{,},{,),*,|,:,>,

Answer

oliver31 picture oliver31 · Nov 25, 2009
Pattern p = Pattern.compile("[^a-z0-9 ]", Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher("I am a string");
boolean b = m.find();

if (b)
   System.out.println("There is a special character in my string");