Java判断字符串是中文还是英文
1个回答
展开全部
Java中判断字符串的编码有两种思路:
一种是根据byte的长度判断,英文的字母数字好标点符号都是一个byte,且值在0-255之间
另一种是根据中文的Unicode取值范围判断,这个就是把所以的范围都包含,才能判断正确,参考unicode中文范围:
示例代码:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class StringTest {
//英文占1byte,非英文(可认为是中文)占2byte,根据这个特性来判断字符
public static boolean checkChar(char ch) {
if ((ch + "").getBytes().length == 1) {
return true;//英文
} else {
return false;//中文
}
}
public static String checkString(String str) {
String res = "";
if (str != null) {
for (int i = 0; i < str.length(); i++) {
//只要字符串中有中文则为中文
if (!checkChar(str.charAt(i))) {
res = "中文";
break;
} else {
res = "英文";
}
}
}
return res;
}
//判断是不是中文
public static boolean isChinese(char c) {
Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
|| ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
|| ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
|| ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
|| ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
|| ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
return true;
}
return false;
}
//判断是不是英文字母
public static boolean isEnglish(String charaString) {
return charaString.matches("^[a-zA-Z]*");
}
//根据中文unicode范围判断u4e00 ~ u9fa5不全
public static String isChinese(String str) {
String regEx1 = "[\\u4e00-\\u9fa5]+";
String regEx2 = "[\\uFF00-\\uFFEF]+";
String regEx3 = "[\\u2E80-\\u2EFF]+";
String regEx4 = "[\\u3000-\\u303F]+";
String regEx5 = "[\\u31C0-\\u31EF]+";
Pattern p1 = Pattern.compile(regEx1);
Pattern p2 = Pattern.compile(regEx2);
Pattern p3 = Pattern.compile(regEx3);
Pattern p4 = Pattern.compile(regEx4);
Pattern p5 = Pattern.compile(regEx5);
Matcher m1 = p1.matcher(str);
Matcher m2 = p2.matcher(str);
Matcher m3 = p3.matcher(str);
Matcher m4 = p4.matcher(str);
Matcher m5 = p5.matcher(str);
if (m1.find() || m2.find() || m3.find() || m4.find() || m5.find())
return "中文";
else
return "英文";
}
public static void main(String[] args) {
System.out.println("使用长度判断:");
System.out.println(checkString("Hello++"));
System.out.println(checkString("Hello++。、,?"));
System.out.println(checkString("Hello++编程"));
System.out.println(checkString("编程"));
System.out.println("\r\n使用正则表达式判断:");
System.out.println(isChinese("Hello++"));
System.out.println(isChinese("Hello++。、,?"));
System.out.println(isChinese("Hello++编程"));
System.out.println(isChinese("编程"));
System.out.println("\r\n使用Character.UnicodeBlock");
System.out.println(isChinese('h')?"中文":"英文");
System.out.println(isChinese(',')?"中文":"英文");
System.out.println(isChinese('。')?"中文":"英文");
System.out.println(isChinese('编')?"中文":"英文");
}
}
一种是根据byte的长度判断,英文的字母数字好标点符号都是一个byte,且值在0-255之间
另一种是根据中文的Unicode取值范围判断,这个就是把所以的范围都包含,才能判断正确,参考unicode中文范围:
示例代码:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class StringTest {
//英文占1byte,非英文(可认为是中文)占2byte,根据这个特性来判断字符
public static boolean checkChar(char ch) {
if ((ch + "").getBytes().length == 1) {
return true;//英文
} else {
return false;//中文
}
}
public static String checkString(String str) {
String res = "";
if (str != null) {
for (int i = 0; i < str.length(); i++) {
//只要字符串中有中文则为中文
if (!checkChar(str.charAt(i))) {
res = "中文";
break;
} else {
res = "英文";
}
}
}
return res;
}
//判断是不是中文
public static boolean isChinese(char c) {
Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
|| ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
|| ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
|| ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
|| ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
|| ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
return true;
}
return false;
}
//判断是不是英文字母
public static boolean isEnglish(String charaString) {
return charaString.matches("^[a-zA-Z]*");
}
//根据中文unicode范围判断u4e00 ~ u9fa5不全
public static String isChinese(String str) {
String regEx1 = "[\\u4e00-\\u9fa5]+";
String regEx2 = "[\\uFF00-\\uFFEF]+";
String regEx3 = "[\\u2E80-\\u2EFF]+";
String regEx4 = "[\\u3000-\\u303F]+";
String regEx5 = "[\\u31C0-\\u31EF]+";
Pattern p1 = Pattern.compile(regEx1);
Pattern p2 = Pattern.compile(regEx2);
Pattern p3 = Pattern.compile(regEx3);
Pattern p4 = Pattern.compile(regEx4);
Pattern p5 = Pattern.compile(regEx5);
Matcher m1 = p1.matcher(str);
Matcher m2 = p2.matcher(str);
Matcher m3 = p3.matcher(str);
Matcher m4 = p4.matcher(str);
Matcher m5 = p5.matcher(str);
if (m1.find() || m2.find() || m3.find() || m4.find() || m5.find())
return "中文";
else
return "英文";
}
public static void main(String[] args) {
System.out.println("使用长度判断:");
System.out.println(checkString("Hello++"));
System.out.println(checkString("Hello++。、,?"));
System.out.println(checkString("Hello++编程"));
System.out.println(checkString("编程"));
System.out.println("\r\n使用正则表达式判断:");
System.out.println(isChinese("Hello++"));
System.out.println(isChinese("Hello++。、,?"));
System.out.println(isChinese("Hello++编程"));
System.out.println(isChinese("编程"));
System.out.println("\r\n使用Character.UnicodeBlock");
System.out.println(isChinese('h')?"中文":"英文");
System.out.println(isChinese(',')?"中文":"英文");
System.out.println(isChinese('。')?"中文":"英文");
System.out.println(isChinese('编')?"中文":"英文");
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询