一、string校验

   要求字符串只能输入数字,字母大小写和‘@‘’‘.’‘_’三个特殊字符

    public static boolean check1(String str){
String pattern = "[\\w@_.]+";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(str);
return m.matches();
} public static boolean check2(String str){
String pattern = "[\\w@_.]+";
return str.matches(pattern);
} public static boolean check3(String str){
String pattern = "[^\\w.@]+";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(str);
return !m.matches();
} public static boolean check4(String str){
String pattern = "[^\\w.@]+";
return !str.matches(pattern);
}

  经验证,check3,check4效率远高于check1,check2,而check4的效率稍高于check3。明显的4个方法中,后两个效率高于前两个是表达式写法的决定的,而后两个方法的效率问题有待考证,暂做记录。

05-14 01:41