判断字符串的格式:
import java.util.regex.Pattern;
public class StringTest {
public static void main(String[] args) {
String str = "A从X";
// 是否全为汉字
if(str.matches("[u4E00-u9FA5]+")){
System.out.println("内容是中文");
}else{
System.out.println("内容包含非中文");
}
// 是否全为数字
if (str.matches("[0-9]*")) {
System.out.println(true);
} else {
System.out.println(false);
}
// 是否全为小写字母
if (str.matches("[a-z]*")) {
System.out.println(true);
} else {
System.out.println(false);
}
// 是否全为大写字母
if (str.matches("[A-Z]*")) {
System.out.println(true);
} else {
System.out.println(false);
}
// 是否含有英文
System.out.println(isENChar(str));
// 是否含有中文
System.out.println(isCNChar(str));
}
/*
是否含有英文
*/
public static boolean isENChar(String string) {
boolean flag = false;
Pattern p = Pattern.compile("[a-zA-z]");
if(p.matcher(string).find()) {
flag = true;
}
return flag;
}
/*
是否含有中文
*/
public static boolean isCNChar(String string){
boolean booleanValue = false;
for(int i=0; i<string.length(); i++){
char c = string.charAt(i);
if(c > 128){
booleanValue = true;
break;
}
}
return booleanValue;
}
}