本文目錄一覽:
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-hant/n/185903.html