package com.utils;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ChineseChar {
public static String regEx = "[\\u4e00-\\u9fa5]";
public static String getChinese(String str) {
String chineseStr="";
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(str);
while (m.find()) {
chineseStr+=m.group(0);
}
return chineseStr;
}
public static boolean checkChinese(String str) {
boolean isChinese=false;
if (str.getBytes().length == str.length()) {
;
} else {
isChinese=true;
}
return isChinese;
}
public static boolean checkAllChinese(String str) {
boolean chFlag=true;
for (int i = 0; i < str.length(); i++) {
char oneChar = str.charAt(i);
if ((oneChar >= '\u4e00' && oneChar <= '\u9fa5') || (oneChar >= '\uf900' && oneChar <= '\ufa2d')){
;
}else{
chFlag=false;
break;
}
}
return chFlag;
}
public static boolean checkNoChinese(String str) {
boolean chFlag=true;
for (int i = 0; i < str.length(); i++) {
char oneChar = str.charAt(i);
if ((oneChar >= '\u4e00' && oneChar <= '\u9fa5') || (oneChar >= '\uf900' && oneChar <= '\ufa2d')){
chFlag=false;
break;
}else{
;
}
}
return chFlag;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
String str1 = "zhong guo";
String str2 = "dfdf中 国。,,";
String str3 = "中国";
ChineseChar ht1 = new ChineseChar();
System.out.println(ht1.checkChinese(str1));
System.out.println(ht1.checkChinese(str2));
System.out.println(ht1.getChinese(str2));
System.out.println(ht1.checkAllChinese(str2));
System.out.println(ht1.checkAllChinese(str3));
System.out.println(ht1.checkNoChinese(str3));
}
}
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ChineseChar {
public static String regEx = "[\\u4e00-\\u9fa5]";
public static String getChinese(String str) {
String chineseStr="";
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(str);
while (m.find()) {
chineseStr+=m.group(0);
}
return chineseStr;
}
public static boolean checkChinese(String str) {
boolean isChinese=false;
if (str.getBytes().length == str.length()) {
;
} else {
isChinese=true;
}
return isChinese;
}
public static boolean checkAllChinese(String str) {
boolean chFlag=true;
for (int i = 0; i < str.length(); i++) {
char oneChar = str.charAt(i);
if ((oneChar >= '\u4e00' && oneChar <= '\u9fa5') || (oneChar >= '\uf900' && oneChar <= '\ufa2d')){
;
}else{
chFlag=false;
break;
}
}
return chFlag;
}
public static boolean checkNoChinese(String str) {
boolean chFlag=true;
for (int i = 0; i < str.length(); i++) {
char oneChar = str.charAt(i);
if ((oneChar >= '\u4e00' && oneChar <= '\u9fa5') || (oneChar >= '\uf900' && oneChar <= '\ufa2d')){
chFlag=false;
break;
}else{
;
}
}
return chFlag;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
String str1 = "zhong guo";
String str2 = "dfdf中 国。,,";
String str3 = "中国";
ChineseChar ht1 = new ChineseChar();
System.out.println(ht1.checkChinese(str1));
System.out.println(ht1.checkChinese(str2));
System.out.println(ht1.getChinese(str2));
System.out.println(ht1.checkAllChinese(str2));
System.out.println(ht1.checkAllChinese(str3));
System.out.println(ht1.checkNoChinese(str3));
}
}
本文介绍了如何使用正则表达式来匹配并提取中文字符,包括一个字符串中所有中文字符的获取方法,以及如何判断一个字符串是否包含中文,进一步探讨了如何检查整个字符串是否全由中文字符组成,同时提供了检测字符串中是否存在非中文字符的功能。
409

被折叠的 条评论
为什么被折叠?



