Java用的是Unicode 編碼char 型變量的范圍是0-65535 無(wú)符號(hào)的值,可以表示 65536個(gè)字符,基本上地球上的字符可被全部包括了
漢字基本集中在[19968,40869]之間,共有20901個(gè)漢字。
unicode編碼范圍:
漢字:[0x4e00,0x9fa5](或十進(jìn)制[19968,40869])
數(shù)字:[0x30,0x39](或十進(jìn)制[48, 57])
小寫字母:[0x61,0x7a](或十進(jìn)制[97, 122])
大寫字母:[0x41,0x5a](或十進(jìn)制[65, 90])
第一種 判斷是否存在漢字
public boolean checkcountname(String countname) { Pattern p = Pattern.compile("[u4e00-u9fa5]"); Matcher m = p.matcher(countname); if (m.find()) { return true; } return false; }
用正則表達(dá)式去匹配
第二種 判斷整個(gè)字符串都由漢字組成
public boolean checkname(String name) { int n = 0; for(int i = 0; i < name.length(); i++) { n = (int)name.charAt(i); if(!(19968 <= n && n <40869)) { return false; } } return true; }