1. java 判斷字元串是否含有中文
Java代碼
package com.zakisoft.ch;
public class IsChineseOrNot {
// GENERAL_PUNCTUATION 判斷中文的「號
// CJK_SYMBOLS_AND_PUNCTUATION 判斷中文的。號
// HALFWIDTH_AND_FULLWIDTH_FORMS 判斷中文的,號
private static final 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 final boolean isChinese(String strName) {
char[] ch = strName.toCharArray();
for (int i = 0; i < ch.length; i++) {
char c = ch[i];
if (isChinese(c)) {
return true;
}
}
return false;
}
public static void main(String[] args) {
System.out.println(isChinese("き"));
System.out.println(isChinese("test,.?!%^&*(){}[]"));
System.out.println(isChinese("測試"));
System.out.println(isChinese("「測試」,。?!%……&*()——{}【】」"));
}
public static final boolean isChineseCharacter(String chineseStr) {
char[] charArray = chineseStr.toCharArray();
for (int i = 0; i < charArray.length; i++) {
if ((charArray[i] >= 0x4e00) && (charArray[i] <= 0x9fbb)) {
return true;
}
}
return false;
}
/**
* @deprecated; 棄用。和方法isChineseCharacter比效率太低。
* */
public static final boolean isChineseCharacter_f2() {
String str = "!?";
for (int i = 0; i < str.length(); i++) {
if (str.substring(i, i + 1).matches("[\\一-\\?]+")) {
return true;
}
}
return false;
}
}
System.out.println(isChinese("き"));
System.out.println(isChinese("test,.?!%^&*(){}[]"));
System.out.println(isChinese("測試"));
System.out.println(isChinese("「測試」,。?!%……&*()——{}【】」"));
運行結果為:
false
false
true
true
2. 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");
}
}
}
}
運行結果如下:
3. Java判斷一個字元串裡面是否含有中文字元
publicstaticbooleanhasChinese(Stringstr){
Stringregex="[u4e00-u9fa5]";
Patternpattern=Pattern.compile(regex);
Matchermatcher=pattern.matcher(str);
booleanflag=false;
if(matcher.find()){
flag=true;
}
returnflag;
}
publicstaticvoidmain(String[]args){
Stringinput="Hello,china!";
System.out.println(hasChinese(input));
input="Hello,中國!";
System.out.println(hasChinese(input));
}
4. Java 判斷字元串中是否含有漢字
Java判斷字元串中是否包含漢字
Java代碼
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class IfHanZi {
public static void main(String[] args) {
//方法一:
String s1 = "我是中國人";
String s2 = "imchinese";
String s3 = "im中國人";
System.out.println(s1 + ":" + new String(s1).length());
System.out.println(s2 + ":" + new String(s2).length());
System.out.println(s3 + ":" + new String(s3).length());
System.out.println((s1.getBytes().length == s1.length()) ? "s1無漢字":"s1有漢字");
System.out.println((s2.getBytes().length == s2.length()) ? "s2無漢字":"s2有漢字");
System.out.println((s3.getBytes().length == s3.length()) ? "s3無漢字":"s3有漢字");
//方法二:
int count = 0;
String regEx = "[\\u4e00-\\u9fa5]";
String str = "中文fd我是中國人as ";
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 + "個 ");
}
}