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

java判斷中文字元

發布時間:2022-10-05 19:25:59

java怎麼判斷輸入的是不是中文

你好,最簡單而且最保險的一種方法是:
這里舉個最復雜的例子:就是輸入了中文和英文的
String str = "....." ; //str是你輸入的東西
如果str.length() < str.getBytes().length 那麼輸入的肯定有中文
看測試代碼:
public class Chinese {
public static void main(String[] args) {
String str1 = "aaa" ;
String str2 = "中國" ;
String str3 = "中a" ;
isChinese(str1) ;
isChinese(str2) ;
isChinese(str3) ;
}
public static void isChinese(String str){
if(str.length() < str.getBytes().length){
System.out.println("有中文");
}else{
System.out.println("英文");
}
}
}

㈡ 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中判斷字元串的編碼有兩種思路:
一種是根據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 判斷字元串中是否有中文符號

學習交流:
622497727

Java判斷一個字元串是否有中文是利用Unicode編碼來判斷,因為中文的編碼區間為:0x4e00--0x9fbb,不過通用區間來判斷中文也不非常精確,因為有些中文的標點符號利用區間判斷會得到錯誤的結果。而且利用區間判斷中文效率也並不高,例如;str.substring(i, i + 1).matches("[\\u4e00-\\u9fbb]+"),就需要遍歷整個字元串,如果字元串太長效率非常低,而且判斷標點還會錯誤

㈤ 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如何判斷一個字元是不是中文的代碼是:

public static boolean isChinese(char c) {

return c >= 0x4E00 && c <= 0x9FA5;// 根據位元組碼判斷

}。

Java是一種可以撰寫跨平台應用軟體的面向對象的程序設計語言。Java 技術具有卓越的通用性、高效性、平台移植性和安全性,廣泛應用於PC、數據中心、游戲控制台、科學超級計算機、行動電話和互聯網,同時擁有全球最大的開發者專業社群。

研發背景

Java是由Sun Microsystems公司推出的Java面向對象程序設計語言(以下簡稱Java語言)和Java平台的總稱。

Java由James Gosling和同事們共同研發,並在1995年正式推出。Java最初被稱為Oak,是1991年為消費類電子產品的嵌入式晶元而設計的。1995年更名為Java,並重新設計用於開發Internet應用程序。用Java實現的HotJava瀏覽器(支持Java applet)顯示了Java的魅力:跨平台、動態Web、Internet計算。

從此,Java被廣泛接受並推動了Web的迅速發展,常用的瀏覽器均支持Javaapplet。另一方面,Java技術也不斷更新。Java自面世後就非常流行,發展迅速,對C++語言形成有力沖擊。在全球雲計算和移動互聯網的產業環境下,Java更具備了顯著優勢和廣闊前景。2010年Oracle公司收購Sun Microsystems。

Java 平台是基於 Java 語言的平台。這樣的平台非常流行。因此微軟公司推出了與之競爭的.NET平台以及模仿Java的C#語言。java的應用已十分廣泛。Java是功能完善的通用程序設計語言,可以用來開發可靠的、要求嚴格的應用程序。

JAVA 的用途:80%以上的高端企業級應用都使用JAVA平台(電信、銀行等)。JAVA是成熟的產品,已經有10年的歷史。

㈦ java判斷一個字元串是否含有中文字元

import java.util.regex.Matcher;import java.util.regex.Pattern;public class Demo { public static void main(String[] args) { System.out.println(isContainChinese("中國123214asdfasd")); } public static boolean isContainChinese(String str) { Pattern p = Pattern.compile("[\u4e00-\u9fa5]"); Matcher m = p.matcher(str); if (m.find()) { return true; } return false; }}

㈧ Java判斷字元串中是否有中文

用字元串編碼來判斷,

㈨ java判斷字元串中是否有中文

把要判斷的字元串放入List裡面,然後遍歷list集合,如果還有指定的字元就輸出,如下代碼:

packagecom.qiu.lin.he;

importjava.util.ArrayList;
importjava.util.List;

publicclassCeshi{
publicstaticvoidmain(String[]args){
List<String>list=newArrayList<String>();//新建一個集合
list.add("puton");
list.add("inonputin");
list.add("oneputonininputoutoutput");

for(Strings:list){
if(s.indexOf("puton")!=-1){//如果含有連續的字元puton則輸出yes
System.out.println(s+"----yes");
}else{
System.out.println(s+"----no");
}

}
}
}

運行結果如下:

㈩ 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判斷中文字元相關的資料

熱點內容
有聲黃讀 瀏覽:542
三大頂級動作片推薦胸大的女人電影 瀏覽:367
哺乳期誘惑電影 瀏覽:706
哪些雲伺服器可以搭建郵件伺服器 瀏覽:687
美國試禁忌當上演員 瀏覽:304
hqss/vod/index.asp 瀏覽:404
男主很帥的歐美大尺度電影 瀏覽:356
怎麼消除安卓的系統通知 瀏覽:478
程序員3萬4是稅後嗎 瀏覽:868
javagui窗口 瀏覽:522
風月片網站動漫 瀏覽:289
電影拉幫套 瀏覽:745
鴉王電影國語版 瀏覽:600
造機甲的黑科技小說 瀏覽:779
歐美在線觀看網站 瀏覽:385
python整蠱代碼大全 瀏覽:458
電影 中國 飛機 瀏覽:103
畫江湖推倒常宣靈小說 瀏覽:158
java表格居中 瀏覽:404
能來回穿梭現代和民國的小說 瀏覽:830