By using below Java code you find out string contains a special character or not?
package com.javapages4all;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class StrRegCheck {
public static void main(String[] args) {
String name="ra2ja2 3ABC 123";
Pattern special= Pattern.compile("[^a-z0-9 ]", Pattern.CASE_INSENSITIVE);
Matcher matcher = special.matcher(name);
if(matcher.find()){
//string contains special symbol/character
System.out.println("string having special character");
} else{
System.out.println("string don't have any special character");
}
}
}
No comments:
Post a Comment