I'm given a String & I need to find whether it contains any special character or not? I tried using charAt()
and ASCII values.But it didn't work.Someone please help.Thanks in advance.
for(int i=0;i<s.length();i++)
{
char c=s.charAt(i);
if(65<=c<=90)
{
System.out.println("Valid");
}
else if(97<=c<=122)
{
System.out.println("Invalid as it contains lower case letter");
break;
}
else if(48<=c<=57)
{
System.out.println("Invalid as it contains numbers");
break;
}
else
System.out.println("Invalid as it contains special characters");
}
Something like this would work :
public static void main(String[] args) {
String s = "asda®††∆˚∆asd121";
System.out.println(s.replaceAll("[a-zA-Z0-9]", ""));
}
O/P :
®††∆˚∆
If the size after replacing is 0, then there were no special characters. You can update the regex with what you consider as special. :)