Android Java如何判断字符串是否包含繁体字?

Android Java如何判断字符串是否包含繁体字?

例如一串字符串 String str = "专业開發者社区";
里面是包含繁体文"開發者",如何识别?

private void isSimpleOrComplex(String txt) {
    for (int i = 0; i < txt.length(); i++) {
        try {
            Charset charset = Charset.forName("MS950");
            ByteBuffer byteBuffer = charset.encode(String.valueOf(txt.charAt(i)));
            byte[] bytes = byteBuffer.array();
            String bytesStr = bytes2HexString(bytes);
            if (bytesStr.compareTo("B0 A1") >= 0 && bytesStr.compareTo("F7 FE") <= 0) {
                Log.e("TAG", "(" + txt + ")" + "包含繁体字");
                return;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    Log.e("TAG", "(" + txt + ")" + "不包含繁体字");
}
isSimpleOrComplex("专业開發者社区");
isSimpleOrComplex("专业开发者社区");

img

public class Demo {
    public static void main(String[] args) throws Exception {
        String encode ="GB2312";
        isSimpleOrComplex("专业開發者社区",encode);
        isSimpleOrComplex("专业开发者社区",encode);
    }

    public static void isSimpleOrComplex(String str, String encode) throws UnsupportedEncodingException {
        if (str.equals(new String(str.getBytes(encode), encode))) {
            System.out.println(str + "---是简体");
        } else {
            System.out.println(str + "---是繁体");
        }
    }
}
  1. 打开github网站
  2. 搜索“繁体”
  3. 语言选择java
  4. 排序选择most stars
  5. 排第一个的库就是你需要的
  6. 参考它的说明加到你的代码里

public static void main(String[] args) {
        String str = "专业開發者社区";
        for(char ch : str.toCharArray()){
            System.out.println(ch + ">>" + isTraditionalChineseCharacter(ch));
        }
    }

    private static boolean isTraditionalChineseCharacter(char c) {
        Character.UnicodeBlock block = Character.UnicodeBlock.of(c);
        if(! Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS.equals(block) &&
                ! Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS.equals(block) &&
                ! Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A.equals(block)) {
            return false;
        }
        try {
            String s = ""+c;
            return s.equals(new String(s.getBytes("MS950"), "MS950"));
        } catch (java.io.UnsupportedEncodingException e) {
            return false;
        }
    }

img


String str = "专业開發者社区";
if (str.equals(new String(str.getBytes("GB2312"),"GB2312")) {
   // 简体
} else {
  // 繁体
}

public static void main(String[] args) throws UnsupportedEncodingException {
String str = "专业開發者社区";
String[] temp = str.split("");
StringBuilder sb = new StringBuilder();
for(int i=0; i<temp.length; i++){
if(!temp[i].equals(new String(temp[i].getBytes("GB2312"), "GB2312"))){
sb.append(temp[i]);
}
}
System.out.println("包含繁体文字:"+sb);
}