{"id":1534,"date":"2015-07-09T15:37:31","date_gmt":"2015-07-09T07:37:31","guid":{"rendered":"https:\/\/cowmanchiang.me\/wp\/?p=1534"},"modified":"2023-10-31T15:43:30","modified_gmt":"2023-10-31T07:43:30","slug":"java-check-string-is-a-messy-code","status":"publish","type":"post","link":"https:\/\/cowmanchiang.me\/wp\/?p=1534","title":{"rendered":"[Java] Check string is a messy code?"},"content":{"rendered":"<p>Ref: <a href=\"http:\/\/blog.icoolxue.com\/java-to-determine-whether-a-string-is-garbled-pro-testing-available\/\">Java\u5224\u65ad\u5b57\u7b26\u4e32\u662f\u5426\u662f\u4e71\u7801\uff08\u4eb2\u6d4b\u53ef\u7528\uff09<\/a><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"\">public static boolean isChinese(char c) {\n    Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);\n    if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS\n            || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS\n            || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A\n            || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION\n            || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION\n            || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {\n        return true;\n    }\n    return false;\n}\n\npublic static boolean isMessyCode(String strName) {\n    Pattern p = Pattern.compile(&quot;\\s*|t*|r*|n*&quot;);\n    Matcher m = p.matcher(strName);\n    String after = m.replaceAll(&quot;&quot;);\n    String temp = after.replaceAll(&quot;\\p{P}&quot;, &quot;&quot;);\n    char[] ch = temp.trim().toCharArray();\n    float chLength = ch.length;\n    float count = 0;\n    for (int i = 0; i &lt; ch.length; i++) {\n        char c = ch[i];\n        if (!Character.isLetterOrDigit(c)) {\n            if (!isChinese(c)) {\n                count = count + 1;\n            }\n        }\n    }\n    float result = count \/ chLength;\n    if (result &gt; 0.4) {\n        return true;\n    } else {\n        return false;\n    }\n\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Ref: Java\u5224\u65ad\u5b57\u7b26\u4e32\u662f\u5426\u662f\u4e71\u7801\uff08\u4eb2\u6d4b\u53ef\u7528\uff09 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; } &hellip; <a href=\"https:\/\/cowmanchiang.me\/wp\/?p=1534\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51],"tags":[],"class_list":["post-1534","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"https:\/\/cowmanchiang.me\/wp\/index.php?rest_route=\/wp\/v2\/posts\/1534","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cowmanchiang.me\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cowmanchiang.me\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cowmanchiang.me\/wp\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cowmanchiang.me\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1534"}],"version-history":[{"count":1,"href":"https:\/\/cowmanchiang.me\/wp\/index.php?rest_route=\/wp\/v2\/posts\/1534\/revisions"}],"predecessor-version":[{"id":1905,"href":"https:\/\/cowmanchiang.me\/wp\/index.php?rest_route=\/wp\/v2\/posts\/1534\/revisions\/1905"}],"wp:attachment":[{"href":"https:\/\/cowmanchiang.me\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1534"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cowmanchiang.me\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1534"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cowmanchiang.me\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1534"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}