1:各種字符轉換
阿新 • • 發佈:2017-11-10
hex count int 是否 trac amp pan stat return
1:計算String字符串中漢字的個數
private int countChinese(String text) { int amount = 0;// 創建統計漢字的計數器 for (int i = 0; i < text.length(); i++) {// 遍歷字符串每一個字符 // 使用正則表達式,判斷字符是否為漢字編碼 boolean matches = Pattern.matches("^[\u4E00-\u9FA5]{0,}$", "" + text.charAt(i)); if (matches) {//如果是漢字 amount++;// 則累加 } } return amount; }
2:十六進制轉為漢字
public static String hexToStringGBK(String s) { byte[] baKeyword = new byte[s.length() / 2]; for (int i = 0; i < baKeyword.length; i++) { try { baKeyword[i]= (byte) (0xff & Integer.parseInt(s.substring(i * 2, i * 2 + 2), 16)); } catch (Exception e) { e.printStackTrace(); return ""; } } try { s = new String(baKeyword, "GBK");// UTF-16le:Not } catch (Exception e1) { e1.printStackTrace();return ""; } return s; }
1:各種字符轉換