判断一个字符串是由字符还是数字、还是汉字组成
1、判断一个字符的类型 代码如下:
/**
* 判断一个字符的类型
*
* @param str
* 需要判断的文本
* @return num 数字 char 英文/拼音 zh 中文 null 出错
*/
public String getCharType(char str) {
String txt = str + "";
Pattern p = Pattern.compile("[0-9]*");
Matcher m = p.matcher(txt);
if (m.matches()) {// 输入的是数字
return "num";
}
p = Pattern.compile("[a-zA-Z]");
m = p.matcher(txt);
if (m.matches()) {// 输入的是字母
return "char";
}
p = Pattern.compile("[\u4e00-\u9fa5]");
m = p.matcher(txt);
if (m.matches()) {// 输入的是汉字
return "zh";
}
return null;// 其实应该抛异常比较合适
}
2、将字符串转化成字符数组,再逐个判断
/**
* 判断一个字符串的类型
*
* @param str
* 字符串
* @return num 数字 char 英文/拼音 zh 中文 null 出错
*/
public String getStringType(String str) {
String type;
char[] chars = str.toCharArray();
type = getCharType(chars[0]);
int i = 1;
for (; i < chars.length; i++) {
if (getCharType(chars[i]) == null) {
return null;
}
if (!(getCharType(chars[i])).equals(type)) {
return null;
}
}
if (i >= chars.length) {
return type;
}
return null;
}
- 如何判断一个字符串是否仅由数字or仅由数字英文字符or仅由汉字组成
- C#中怎样判断一个字符串中得字符是汉字还是字母
- js判断输入字符串长度(汉字算两个字符,字母数字算一个)
- js脚本系列(1) JS判断输入字符串长度(汉字算两个字符,字母数字算一个)
- JS判断输入字符串长度(汉字算两个字符,字母数字算一个)
- JS判断输入字符串长度实例代码(汉字算两个字符,字母数字算一个)
- VC判断一个UNICODE字符串中字母、数字、汉字、其他字符的个数
- js判断输入字符串长度(汉字算两个字符,字母数字算一个)
- JS判断输入字符串长度(汉字算两个字符,字母数字算一个)
- js判断输入字符串长度(汉字算两个字符,字母数字算一个)
- js判断输入字符串长度(汉字算两个字符,字母数字算一个)
- 输入一个字符判断该字符为数字,大写字母,小写字母,还是其他字符
- 华为2写出一个程序,接受一个有字母和数字以及空格组成的字符串,和一个字符,然后输出输入字符串中含有该字符的个数。不区分大小写。
- JS判断一个字符是汉字还是字母
- 判断字符串是否仅为数字/首字符是否为字母/汉字
- ORD 函数可以取得字符的ASCII码的值 判断一个字符是汉字还是
- 判断一个字符串String是否是由数字组成,采用正则表达式
- 如何判断一个字符串中是否含有汉字或汉字标点(即双字节字符)
- php---------正则判断字符串中是否由汉字 数字 英文字母组成
- 接受一个有字母和数字以及空格组成的字符串,和一个字符,然后输出输入字符串中含有该字符的个数。不区分大小写。