• 【转】Java检测字符串是否有乱码


    package cn.cnnic.ops.learn;

    import java.util.regex.Matcher;
    import java.util.regex.Pattern;

    public class Luanma {
    /**
    * 判断字符是否是中文
    *
    * @param c 字符
    * @return 是否是中文
    */
    public static boolean isChinese(char c) {
    Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
    if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
    || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
    || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
    || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
    || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
    || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
    return true;
    }
    return false;
    }

    /**
    * 判断字符串是否是乱码
    *
    * @param strName 字符串
    * @return 是否是乱码
    */
    public static boolean isMessyCode(String strName) {
    Pattern p = Pattern.compile("\s*|t*|r*|n*");
    Matcher m = p.matcher(strName);
    String after = m.replaceAll("");
    String temp = after.replaceAll("\p{P}", "");
    char[] ch = temp.trim().toCharArray();
    float chLength = ch.length;
    float count = 0;
    for (int i = 0; i < ch.length; i++) {
    char c = ch[i];
    if (!Character.isLetterOrDigit(c)) {
    if (!isChinese(c)) {
    count = count + 1;
    }
    }
    }
    float result = count / chLength;
    if (result > 0.4) {
    return true;
    } else {
    return false;
    }

    }

    public static void main(String[] args) {
    System.out.println(isMessyCode("韩顺平"));
    System.out.println(isMessyCode("abcd"));
    System.out.println(isMessyCode("你好"));
    }
    }

    【转自】http://blog.icoolxue.com/java-to-determine-whether-a-string-is-garbled-pro-testing-available/

    放着,待用。

  • 相关阅读:
    联赛前第五阶段总结
    陶陶摘苹果 —— 线段树维护单调栈
    联赛前第三阶段总结
    联赛前第四阶段总结
    [NOIP
    超级跳马 —— 矩阵快速幂优化DP
    我的博客园美化
    Wedding —— 2-SAT
    C++运算符优先级
    water——小根堆+BFS
  • 原文地址:https://www.cnblogs.com/zhzhang/p/5543309.html
Copyright © 2020-2023  润新知