package util;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class StringUtils {
/**
* @Description 字符串是否是中文
* @date 2018年11月12日下午3:50:16
*/
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;
}
/**
* @Description 字符串是否是乱码
* @date 2018年11月12日下午3:49:48
*/
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;
}
}
/**
* @Description 字符串是否包含乱码
* @date 2018年11月12日下午3:45:49
*/
public static boolean isIncloudMessyCode(String str) {
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if ((int) c == 0xfffd) {
return true;
}
}
return false;
}
/**
* @Description 去除emoji表情
* @date 2018年11月12日下午3:46:11
*/
public static String removeEmoji(String str) {
return str.replaceAll("[\\ud800\\udc00-\\udbff\\udfff\\ud800-\\udfff]", "*");
}
/**
* @Description 去除回车键
* @date 2018年11月12日下午3:48:19
*/
public static String removeEnter(String str) {
return str.replaceAll("\n", "");
}
/**
* @Description URLEncode解码
* @date 2018年11月12日下午4:00:23
*/
public static String decodeUrl(String str) {
try {
return java.net.URLDecoder.decode(str, "utf-8");
} catch (UnsupportedEncodingException e) {
return "";
}
}
}