java判斷中文,java判斷中文亂碼

本文目錄一覽:

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 + “個 “);

} ——————————————————————-

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/185903.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-11-26 21:08
下一篇 2024-11-26 21:08

相關推薦

  • java client.getacsresponse 編譯報錯解決方法

    java client.getacsresponse 編譯報錯是Java編程過程中常見的錯誤,常見的原因是代碼的語法錯誤、類庫依賴問題和編譯環境的配置問題。下面將從多個方面進行分析…

    編程 2025-04-29
  • Java JsonPath 效率優化指南

    本篇文章將深入探討Java JsonPath的效率問題,並提供一些優化方案。 一、JsonPath 簡介 JsonPath是一個可用於從JSON數據中獲取信息的庫。它提供了一種DS…

    編程 2025-04-29
  • Java騰訊雲音視頻對接

    本文旨在從多個方面詳細闡述Java騰訊雲音視頻對接,提供完整的代碼示例。 一、騰訊雲音視頻介紹 騰訊雲音視頻服務(Cloud Tencent Real-Time Communica…

    編程 2025-04-29
  • Java Bean加載過程

    Java Bean加載過程涉及到類加載器、反射機制和Java虛擬機的執行過程。在本文中,將從這三個方面詳細闡述Java Bean加載的過程。 一、類加載器 類加載器是Java虛擬機…

    編程 2025-04-29
  • Java Milvus SearchParam withoutFields用法介紹

    本文將詳細介紹Java Milvus SearchParam withoutFields的相關知識和用法。 一、什麼是Java Milvus SearchParam without…

    編程 2025-04-29
  • Java 8中某一周的周一

    Java 8是Java語言中的一個版本,於2014年3月18日發佈。本文將從多個方面對Java 8中某一周的周一進行詳細的闡述。 一、數組處理 Java 8新特性之一是Stream…

    編程 2025-04-29
  • Python zipfile解壓文件亂碼處理

    本文主要介紹如何在Python中使用zipfile進行文件解壓的處理,同時詳細討論在解壓文件時可能出現的亂碼問題的各種解決辦法。 一、zipfile解壓文件亂碼問題的根本原因 在P…

    編程 2025-04-29
  • Java判斷字符串是否存在多個

    本文將從以下幾個方面詳細闡述如何使用Java判斷一個字符串中是否存在多個指定字符: 一、字符串遍歷 字符串是Java編程中非常重要的一種數據類型。要判斷字符串中是否存在多個指定字符…

    編程 2025-04-29
  • VSCode為什麼無法運行Java

    解答:VSCode無法運行Java是因為默認情況下,VSCode並沒有集成Java運行環境,需要手動添加Java運行環境或安裝相關插件才能實現Java代碼的編寫、調試和運行。 一、…

    編程 2025-04-29
  • Java任務下發回滾系統的設計與實現

    本文將介紹一個Java任務下發回滾系統的設計與實現。該系統可以用於執行複雜的任務,包括可回滾的任務,及時恢復任務失敗前的狀態。系統使用Java語言進行開發,可以支持多種類型的任務。…

    編程 2025-04-29

發表回復

登錄後才能評論