Java中斷定字符串是中文或許英文的對象類分享。本站提示廣大學習愛好者:(Java中斷定字符串是中文或許英文的對象類分享)文章只能為提供參考,不一定能成為您想要的結果。以下是Java中斷定字符串是中文或許英文的對象類分享正文
直接上代碼:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
*
* <p>
* ClassName ShowChineseInUnicodeBlock
* </p>
* <p>
* Description 供給斷定字符串是中文或許是英文的一種思緒
* </p>
*
* @author wangxu [email protected]
* <p>
* Date 2014-9-16 下晝06:45:35
* </p>
* @version V1.0
*
*/
public class ShowChineseInUnicodeBlock {
public static void main(String[] args) {
String str = "我愛你,!?():;“”、。";
char[] charArray = str.toCharArray();
for (int i = 0; i < charArray.length; i++) {
isChinese(charArray[i]);
}
String chinese = "中國god damn";
System.out.println(isContainChinese(chinese));
String english = "dfafdabac";
System.out.println(isEnglish(english));
}
/**
*
* <p>
* Title: isChinese
* </p>
* <p>
* Description: 該函數就用來打印一些字符看看屬於甚麼
* </p>
*
* @param c
*
*/
public static void isChinese(char c) {
Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS) {
System.out.println(c + "--CJK_UNIFIED_IDEOGRAPHS");
} else if (ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS) {
System.out.println(c + "--CJK_COMPATIBILITY_IDEOGRAPHS");
} else if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A) {
// CJK Unified Ideographs Extension WikipediaUnicode擴大漢字
// CJK Unified Ideographs Extension A 中日韓同一表意文字擴大區A ; 表意文字擴大A
// CJK Unified Ideographs Extension B 中日韓同一表意文字擴大區B
System.out.println(c + "--CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A");
} else if (ub == Character.UnicodeBlock.GENERAL_PUNCTUATION) {// 通用標點
System.out.println(c + "--GENERAL_PUNCTUATION");
} else if (ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION) {
System.out.println(c + "--CJK_SYMBOLS_AND_PUNCTUATION");
} else if (ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
System.out.println(c + "--HALFWIDTH_AND_FULLWIDTH_FORMS");
}
}
public static boolean isEnglish(String charaString) {
return charaString.matches("^[a-zA-Z]*");
}
public static boolean isContainChinese(String str) {// 檢測能否包括中文
String regEx = "[\\u4E00-\\u9FA5]+";
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(str);
if (m.find()) {
return true;
} else {
return false;
}
}
}