区别输入的字符是英文的还是中文 或者是 特殊字符

2014-11-24 11:49:50 · 作者: · 浏览: 11

区别中文和英文字符的方法:


[java]
String str = "我爱你,xr";
char[] array = str.toCharArray();
int chineseCount = 0;
int englishCount = 0;
for (int i = 0; i < array.length; i++) {
if((char)(byte)array[i]!=array[i]){
chineseCount++;
}else{
englishCount++;
}
}

String str = "我爱你,xr";
char[] array = str.toCharArray();
int chineseCount = 0;
int englishCount = 0;
for (int i = 0; i < array.length; i++) {
if((char)(byte)array[i]!=array[i]){
chineseCount++;
}else{
englishCount++;
}
}
判断是否包含特殊字符的方法:


[java]
Pattern p = Pattern.compile("[&%$#@!(),*^]"); //可在此加上其他的特殊字符
Matcher m = p.matcher(str);
if (m.find()) {
Toast.makeText(getApplicationContext(),"you can not enter special Character", 1).show();
} else {
Toast.makeText(getApplicationContext(),"input ok", 1).show();
}

Pattern p = Pattern.compile("[&%$#@!(),*^]"); //可在此加上其他的特殊字符
Matcher m = p.matcher(str);
if (m.find()) {
Toast.makeText(getApplicationContext(),"you can not enter special Character", 1).show();
} else {
Toast.makeText(getApplicationContext(),"input ok", 1).show();
}