中文乱码在项目中是经常会遇到的情况。当我们通过设置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; i0.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 /*Listlist = new ArrayList (); Map , String> map = new HashMap<>();*/ String messcode = "ss201888asdf;#????"; System.out.println(">>>>>>>>>>:"+isMessyCode(messcode)); }
输出结果:
>>>>>>>>>>:false
更多java知识请关注java基础教程栏目。
以上就是java如何判断是否中文乱码的详细内容,更多请关注其它相关文章!