代碼很簡單,把字符串型的漢字轉(zhuǎn)換成字符型,然后強轉(zhuǎn)為int型即可。
網(wǎng)站建設(shè)哪家好,找成都創(chuàng)新互聯(lián)!專注于網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、小程序設(shè)計、集團企業(yè)網(wǎng)站建設(shè)等服務(wù)項目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了海陽免費建站歡迎大家使用!
Scanner?sc?=?new?Scanner(System.in);
System.out.println("請輸入一個字符:");
String?input?=?sc.nextLine();
if?(input?==?null?||?input.isEmpty())?{
System.out.println("輸入為空");
return;
}
char?c?=?input.charAt(0);?//?如果輸入了多個漢字只取第一個
System.out.println("字符'"?+?c?+?"'在unicode表中的位置為:"?+?(int)?c);
樓主來瞧瞧我寫的 測試已經(jīng)通過 代碼如下 直接復(fù)制就可以運行
import java.util.Random;
public class test {
public static void main(String[] args) {
Random r = new Random();
String random =r.nextInt(9999)+"";
String a = CodeToChinese(random);
System.out.println("隨機數(shù)是 :"+random);
System.out.println("根據(jù)隨機的區(qū)位碼得出的是:"+a);
}
public static String CodeToChinese(String code) {
String Chinese = " ";
for (int i = 0; i code.length(); i += 4) {
byte[] bytes = new byte[2];
String lowCode = code.substring(i, i + 2);
int tempLow = Integer.parseInt(lowCode, 16);
tempLow += 160;
bytes[0] = (byte) tempLow;
String highCode = code.substring(i + 2, i + 4);
int tempHigh = Integer.parseInt(highCode, 16);
tempHigh += 160;
bytes[1] = (byte) tempHigh;
String chara = new String(bytes);
Chinese += chara;
}
return Chinese;
}
}
可以使用正則表達式+中文字符編碼區(qū)間驗證一個字符串中是否包含漢字
代碼如下:
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 + "個 ");
}
文章標(biāo)題:漢子在java代碼中查詢 漢子在java代碼中查詢不到
文章網(wǎng)址:http://vcdvsql.cn/article30/doichso.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、響應(yīng)式網(wǎng)站、建站公司、網(wǎng)站內(nèi)鏈、用戶體驗、服務(wù)器托管
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)