本文目录一览:
java判断字符是不是中文
下满给出示例代码,希望对你有帮助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('编')?"中文":"英文"); }}运行结果:使用长度判断:英文中文中文中文使用正则表达式判断:英文中文中文中文使用Character.UnicodeBlock英文英文中文中文
Java判断字符串中是否有中文
public static boolean isChinese(char c) {
return c = 0x4E00 c = 0x9FA5;// 根据字节码判断
}
// 判断一个字符串是否含有中文
public static boolean isChinese(String str) {
if (str == null) return false;
for (char c : str.toCharArray()) {
if (isChinese(c)) return true;// 有一个中文字符就返回
}
return false;
}
用字符串编码来判断,
Java判断字符串是中文还是英文
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('编')?"中文":"英文");
}
}
java 判断字符是否为汉字
java判断是否为汉字 Java代码如下:
public boolean vd(String str){
char[] chars=str.toCharArray();
boolean isGB2312=false;
for(int i=0;ichars.length;i++){
byte[] bytes=(""+chars[i]).getBytes();
if(bytes.length==2){
int[] ints=new int[2];
ints[0]=bytes[0] 0xff;
ints[1]=bytes[1] 0xff;
if(ints[0]=0x81 ints[0]=0xFE ints[1]=0x40 ints[1]=0xFE){
isGB2312=true;
break;
}
}
}
return isGB2312;
}
public boolean vd(String str){
char[] chars=str.toCharArray();
boolean isGB2312=false;
for(int i=0;ichars.length;i++){
byte[] bytes=(""+chars[i]).getBytes();
if(bytes.length==2){
int[] ints=new int[2];
ints[0]=bytes[0] 0xff;
ints[1]=bytes[1] 0xff;
if(ints[0]=0x81 ints[0]=0xFE ints[1]=0x40 ints[1]=0xFE){
isGB2312=true;
break;
}
}
}
return isGB2312;
}
首先要import java.util.regex.Pattern 和 java.util.regex.Matcher
这两个包,接下来是代码
Java代码
public boolean isNumeric(String str)
{
Pattern pattern = Pattern.compile(”[0-9]*”);
Matcher isNum = pattern.matcher(str);
if( !isNum.matches() ) {
return false;
}
return true;
}
java.lang.Character.isDigit(ch[0])
public boolean isNumeric(String str)
{
Pattern pattern = Pattern.compile(”[0-9]*”);
Matcher isNum = pattern.matcher(str);
if( !isNum.matches() ) {
return false;
}
return true;
}
java.lang.Character.isDigit(ch[0])
-----------------另一种-----------------
Java代码
public static void main(String[] args) {
int count = 0;
String regEx = "[\\u4e00-\\u9fa5]";
//System.out.println(regEx);
String str = "中文fdas ";
//System.out.println(str);
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(str);
while (m.find()) {
for (int i = 0; i = m.groupCount(); i++) {
count = count + 1;
}
}
System.out.println("共有 " + count + "个 ");
}
public static void main(String[] args) {
int count = 0;
String regEx = "[\\u4e00-\\u9fa5]";
//System.out.println(regEx);
String str = "中文fdas ";
//System.out.println(str);
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(str);
while (m.find()) {
for (int i = 0; i = m.groupCount(); i++) {
count = count + 1;
}
}
System.out.println("共有 " + count + "个 ");
} -------------------------------------------------------------------