中文乱码在项目中是经常会遇到的情况。当我们通过设置request和response字符集,但是还是出现中文乱码的时候,今天给介绍一种通过java方法实现判断string是否为乱码的方法。
/** * 判断字符串是否是乱码 * * @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; } } /** * 判断字符是否是中文 * * @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; }
定义调用:
public static void main(String[] args) { // TODO Auto-generated method stub /*List<String> list = new ArrayList<String>(); Map<List<String>, String> map = new HashMap<>();*/ String messcode = "ss201888asdf;#????"; System.out.println(">>>>>>>>>>:"+isMessyCode(messcode)); }
输出结果:
>>>>>>>>>>:false
更多java知识请关注PHP中文网java基础教程栏目。
以上是java如何判斷是否中文亂碼的詳細內容。更多資訊請關注PHP中文網其他相關文章!