By arunraj
Use the following code in Java to check whether a given String contains any special characters or not.
public boolean isContainsSpecialChar(String toCheck)
{
boolean isContainsSC = false;
if (toCheck != null && !toCheck.equals(""))
{
Matcher m = Pattern.compile("[\\W]").matcher(toCheck);
while (m.find())
{
isContainsSC = true;
System.out.println(m.start());
System.out.println(m.group());
}
}
return isContainsSC;
}
{
boolean isContainsSC = false;
if (toCheck != null && !toCheck.equals(""))
{
Matcher m = Pattern.compile("[\\W]").matcher(toCheck);
while (m.find())
{
isContainsSC = true;
System.out.println(m.start());
System.out.println(m.group());
}
}
return isContainsSC;
}