導航:首頁 > 編程語言 > java判斷字元為中文

java判斷字元為中文

發布時間:2022-06-24 00:48:00

㈠ 在java中如何判斷一個字元串是中文的還是英文的

下滿給出示例代碼,希望對你有幫助
Java中判斷字元串的編碼有兩種思路:
一種是根據byte的長度判斷,英文的字母數字好標點符號都是一個byte,且值在0-255之間
另一種是根據中文的Unicode取值范圍判斷,這個就是把所以的范圍都包含,才能判斷正確,參考unicode中文范圍:

示例代碼:
importjava.util.regex.Matcher;
importjava.util.regex.Pattern;
publicclassStringTest{
//英文佔1byte,非英文(可認為是中文)佔2byte,根據這個特性來判斷字元
publicstaticbooleancheckChar(charch){
if((ch+"").getBytes().length==1){
returntrue;//英文
}else{
returnfalse;//中文
}
}
publicstaticStringcheckString(Stringstr){
Stringres="";
if(str!=null){
for(inti=0;i<str.length();i++){
//只要字元串中有中文則為中文
if(!checkChar(str.charAt(i))){
res="中文";
break;
}else{
res="英文";
}
}
}
returnres;
}
//判斷是不是中文
publicstaticbooleanisChinese(charc){
Character.UnicodeBlockub=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){
returntrue;
}
returnfalse;
}
//判斷是不是英文字母
publicstaticbooleanisEnglish(StringcharaString){
returncharaString.matches("^[a-zA-Z]*");
}
//根據中文unicode范圍判斷u4e00~u9fa5不全
publicstaticStringisChinese(Stringstr){
StringregEx1="[\u4e00-\u9fa5]+";
StringregEx2="[\uFF00-\uFFEF]+";
StringregEx3="[\u2E80-\u2EFF]+";
StringregEx4="[\u3000-\u303F]+";
StringregEx5="[\u31C0-\u31EF]+";
Patternp1=Pattern.compile(regEx1);
Patternp2=Pattern.compile(regEx2);
Patternp3=Pattern.compile(regEx3);
Patternp4=Pattern.compile(regEx4);
Patternp5=Pattern.compile(regEx5);
Matcherm1=p1.matcher(str);
Matcherm2=p2.matcher(str);
Matcherm3=p3.matcher(str);
Matcherm4=p4.matcher(str);
Matcherm5=p5.matcher(str);
if(m1.find()||m2.find()||m3.find()||m4.find()||m5.find())
return"中文";
else
return"英文";
}
publicstaticvoidmain(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(" 使用正則表達式判斷:");
System.out.println(isChinese("Hello++"));
System.out.println(isChinese("Hello++。、,?"));
System.out.println(isChinese("Hello++編程"));
System.out.println(isChinese("編程"));

System.out.println(" 使用Character.UnicodeBlock");
System.out.println(isChinese('h')?"中文":"英文");
System.out.println(isChinese(',')?"中文":"英文");
System.out.println(isChinese('。')?"中文":"英文");
System.out.println(isChinese('編')?"中文":"英文");
}
}

㈡ Java判斷字元是否是漢字

public static void main(String[] args) {
Pattern p_str = Pattern.compile("[\\u4e00-\\u9fa5]+");
String str="我是漢字";
Matcher m = p_str.matcher(str);
if(m.find()&&m.group(0).equals(str)){
System.out.println("Ok");
}
} 這個就可以判斷,輸入任意個字元,必須所有的都是中文,才算是

㈢ 在java中如何判斷一段字元串是否為中文

"^[\u4e00-\u9fbb]+$";
^表示從字元串開頭開始檢測,
$表示檢測到字元串結尾結束,
[\u4e00-\u9fbb]表示中文在其對應編碼中的取值范圍
+表示出現1次或者多次

㈣ java判斷一個字元串是中文還是英文

參考博文:http://www.yuanxingyuan.com/?id=15

下滿給出示例代碼,希望對你有幫助

Java中判斷字元串的編碼有兩種思路:

一種是根據byte的長度判斷,英文的字母數字好標點符號都是一個byte,且值在0-255之間

另一種是根據中文的Unicode取值范圍判斷,這個就是把所以的范圍都包含,才能判斷正確,參考unicode中文范圍:http://www.yuanxingyuan.com/?id=14

示例代碼:

importjava.util.regex.Matcher;
importjava.util.regex.Pattern;
publicclassStringTest{
//英文佔1byte,非英文(可認為是中文)佔2byte,根據這個特性來判斷字元
publicstaticbooleancheckChar(charch){
if((ch+"").getBytes().length==1){
returntrue;//英文
}else{
returnfalse;//中文
}
}
publicstaticStringcheckString(Stringstr){
Stringres="";
if(str!=null){
for(inti=0;i<str.length();i++){
//只要字元串中有中文則為中文
if(!checkChar(str.charAt(i))){
res="中文";
break;
}else{
res="英文";
}
}
}
returnres;
}
//判斷是不是中文
publicstaticbooleanisChinese(charc){
Character.UnicodeBlockub=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){
returntrue;
}
returnfalse;
}
//判斷是不是英文字母
publicstaticbooleanisEnglish(StringcharaString){
returncharaString.matches("^[a-zA-Z]*");
}
//根據中文unicode范圍判斷u4e00~u9fa5不全
publicstaticStringisChinese(Stringstr){
StringregEx1="[\u4e00-\u9fa5]+";
StringregEx2="[\uFF00-\uFFEF]+";
StringregEx3="[\u2E80-\u2EFF]+";
StringregEx4="[\u3000-\u303F]+";
StringregEx5="[\u31C0-\u31EF]+";
Patternp1=Pattern.compile(regEx1);
Patternp2=Pattern.compile(regEx2);
Patternp3=Pattern.compile(regEx3);
Patternp4=Pattern.compile(regEx4);
Patternp5=Pattern.compile(regEx5);
Matcherm1=p1.matcher(str);
Matcherm2=p2.matcher(str);
Matcherm3=p3.matcher(str);
Matcherm4=p4.matcher(str);
Matcherm5=p5.matcher(str);
if(m1.find()||m2.find()||m3.find()||m4.find()||m5.find())
return"中文";
else
return"英文";
}
publicstaticvoidmain(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(" 使用正則表達式判斷:");
System.out.println(isChinese("Hello++"));
System.out.println(isChinese("Hello++。、,?"));
System.out.println(isChinese("Hello++編程"));
System.out.println(isChinese("編程"));

System.out.println(" 使用Character.UnicodeBlock");
System.out.println(isChinese('h')?"中文":"英文");
System.out.println(isChinese(',')?"中文":"英文");
System.out.println(isChinese('。')?"中文":"英文");
System.out.println(isChinese('編')?"中文":"英文");
}
}

運行結果:

使用長度判斷:
英文
中文
中文
中文

使用正則表達式判斷:
英文
中文
中文
中文

使用Character.UnicodeBlock
英文
英文
中文
中文

㈤ java判斷字元是不是中文

下滿給出示例代碼,希望對你有幫助Java中判斷字元串的編碼有兩種思路:一種是根據byte的長度判斷,英文的字母數字好標點符號都是一個byte,且值在0-255之間另一種是根據中文的Unicode取值范圍判斷,這個就是把所以的范圍都包含,才能判斷正確,參考unicode中文范圍:http://www.yuanxingyuan.com/?id=14示例代碼: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 判斷字元是否為漢字

java判斷是否為漢字 Java代碼如下:
public boolean vd(String str){

char[] chars=str.toCharArray();
boolean isGB2312=false;
for(int i=0;i<chars.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;i<chars.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 + "個 ");
} -------------------------------------------------------------------

㈦ java怎麼檢驗字元串是否全為純中文

Java判斷一個字元串是否有中文一般情況是利用Unicode編碼(CJK統一漢字的編碼區間:0x4e00–0x9fbb)的正則來做判斷,但是其實這個區間來判斷中文不是非常...

㈧ java判斷字元串中是否含有中文

publicclasstest{

publicstaticvoidmain(String[]args){
System.out.println(isContainsChinese("122地點"));
}

//方法返回true為包含中文;false不包含
(Stringstr)
{
Patternpat=Pattern.compile("[u4e00-u9fa5]");
Matchermatcher=pat.matcher(str);
booleanflg=false;
if(matcher.find()){
flg=true;
}
returnflg;
}
}

㈨ java判斷一個字元是不是漢字

在英文狀態半月下打的就是半個字元,在全圓的狀態下是一個字元的

閱讀全文

與java判斷字元為中文相關的資料

熱點內容
代碼加密常用方法 瀏覽:952
安卓手機如何解除已禁用 瀏覽:396
演算法的隨機性 瀏覽:485
高中解壓體育游戲 瀏覽:532
androidstudior丟失 瀏覽:345
命令行筆記 瀏覽:737
360目標文件夾訪問拒絕 瀏覽:518
3b編程加工指令 瀏覽:789
c8051f系列單片機選型手冊 瀏覽:772
南昌php程序員 瀏覽:511
bcs命令 瀏覽:446
如何在伺服器指向域名 瀏覽:417
車床編程可以做刀嗎 瀏覽:519
ln命令源碼 瀏覽:791
用粘液做解壓手套 瀏覽:331
icloud收信伺服器地址 瀏覽:500
編程思考者 瀏覽:453
壓縮機型號用什麼氟利昂 瀏覽:553
農機空氣壓縮機 瀏覽:666
程序員下載歌曲 瀏覽:897