码迷,mamicode.com
首页 > 编程语言 > 详细

java 判断字符串是否为乱码

时间:2015-06-16 16:32:37      阅读:260      评论:0      收藏:0      [点我收藏+]

标签:

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

public class MessyCodeCheck {

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 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;
System.out.print(c);
}
}
}
float result = count / chLength;
if (result > 0.4) {
return true;
} else {
return false;
}

}

public static void main(String[] args) {
System.out.println(isMessyCode("*??JTP.jar??????JTP?????????????"));
System.out.println(isMessyCode("你好"));
}
}

java 判断字符串是否为乱码

标签:

原文地址:http://www.cnblogs.com/zhangzhifeng/p/4580803.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!