Java中判断字符串是中文或者英文的工具类分享
直接上代码:
import java.util.regex.matcher;
import java.util.regex.pattern;
/**
*
* <p>
* classname showchineseinunicodeblock
* </p>
* <p>
* description 提供判断字符串是中文或者是英文的一种思路
* </p>
*
* @author wangxu wangx89@126.com
* <p>
* date 2014-9-16 下午06:45:35
* </p>
* @version v1.0
*
*/
public class showchineseinunicodeblock {
public static void main(string[] args) {
string str = "我爱你,!?():;“”、。";
char[] chararray = str.tochararray();
for (int i = 0; i < chararray.length; i++) {
ischinese(chararray[i]);
}
string chinese = "中国god damn";
system.out.println(iscontainchinese(chinese));
string english = "dfafdabac";
system.out.println(isenglish(english));
}
/**
*
* <p>
* title: ischinese
* </p>
* <p>
* description: 该函数就用来打印一些字符看看属于什么
* </p>
*
* @param c
*
*/
public static void ischinese(char c) {
character.unicodeblock ub = character.unicodeblock.of(c);
if (ub == character.unicodeblock.cjk_unified_ideographs) {
system.out.println(c + "--cjk_unified_ideographs");
} else if (ub == character.unicodeblock.cjk_compatibility_ideographs) {
system.out.println(c + "--cjk_compatibility_ideographs");
} else if (ub == character.unicodeblock.cjk_unified_ideographs_extension_a) {
// cjk unified ideographs extension wikipediaunicode扩展汉字
// cjk unified ideographs extension a 中日韩统一表意文字扩展区a ; 表意文字扩充a
// cjk unified ideographs extension b 中日韩统一表意文字扩展区b
system.out.println(c + "--cjk_unified_ideographs_extension_a");
} else if (ub == character.unicodeblock.general_punctuation) {// 通用标点
system.out.println(c + "--general_punctuation");
} else if (ub == character.unicodeblock.cjk_symbols_and_punctuation) {
system.out.println(c + "--cjk_symbols_and_punctuation");
} else if (ub == character.unicodeblock.halfwidth_and_fullwidth_forms) {
system.out.println(c + "--halfwidth_and_fullwidth_forms");
}
}
public static boolean isenglish(string charastring) {
return charastring.matches("^[a-za-z]*");
}
public static boolean iscontainchinese(string str) {// 检测是否包含中文
string regex = "[\\u4e00-\\u9fa5]+";
pattern p = pattern.compile(regex);
matcher m = p.matcher(str);
if (m.find()) {
return true;
} else {
return false;
}
}
}