|
@@ -1,5 +1,7 @@
|
|
|
package com.ywt.biz.common.util;
|
|
|
|
|
|
+import java.security.MessageDigest;
|
|
|
+import java.security.NoSuchAlgorithmException;
|
|
|
import java.util.UUID;
|
|
|
import java.util.regex.Matcher;
|
|
|
import java.util.regex.Pattern;
|
|
@@ -11,11 +13,6 @@ public class StringHelper {
|
|
|
public static final String scriptReg = "<script[\\s\\S]+?</script>";
|
|
|
public static final String iframeReg = "<iframe[\\s\\S]+?</iframe>";
|
|
|
|
|
|
- /**
|
|
|
- * 非负整数正则表达式
|
|
|
- */
|
|
|
- private static final String NON_NEGATIVE_INTEGER_REGEX = "^\\d+$";
|
|
|
-
|
|
|
public static String clearHtmlTag(String content) {
|
|
|
if (!isNullOrEmpty(content)) {
|
|
|
content = clearSpecialContent(content, scriptReg);
|
|
@@ -48,7 +45,7 @@ public class StringHelper {
|
|
|
return sourceStr;
|
|
|
}
|
|
|
|
|
|
- public static String substringContent(String sourceStr, String regexExp, int flags){
|
|
|
+ public static String substringContent(String sourceStr, String regexExp, int flags) {
|
|
|
Pattern pattern = Pattern.compile(regexExp, flags);
|
|
|
Matcher matcher = pattern.matcher(sourceStr);
|
|
|
return matcher.find() ? matcher.group() : "";
|
|
@@ -60,26 +57,25 @@ public class StringHelper {
|
|
|
|
|
|
/**
|
|
|
* 判断传入的value是null还是空白的字符串
|
|
|
+ *
|
|
|
* @param value
|
|
|
* @return
|
|
|
*/
|
|
|
- public static boolean isNullOrWhiteSpace(String value){
|
|
|
- return value == null || "".equals(value)||"".equals(value.trim());
|
|
|
+ public static boolean isNullOrWhiteSpace(String value) {
|
|
|
+ return value == null || "".equals(value) || "".equals(value.trim());
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 转半角的函数
|
|
|
+ *
|
|
|
* @param value 任意字符串
|
|
|
* @return 半角字符串
|
|
|
*/
|
|
|
- public static String toDBC(String value)
|
|
|
- {
|
|
|
+ public static String toDBC(String value) {
|
|
|
char[] c = value.toCharArray();
|
|
|
- for (int i = 0; i < c.length; i++)
|
|
|
- {
|
|
|
- if (c[i] == 12288)
|
|
|
- {
|
|
|
+ for (int i = 0; i < c.length; i++) {
|
|
|
+ if (c[i] == 12288) {
|
|
|
c[i] = (char) 32;
|
|
|
continue;
|
|
|
}
|
|
@@ -91,17 +87,18 @@ public class StringHelper {
|
|
|
|
|
|
/**
|
|
|
* 获取没有连字号的UUID
|
|
|
+ *
|
|
|
* @return
|
|
|
*/
|
|
|
- public static String getUUIDWithoutHyphen(){
|
|
|
+ public static String getUUIDWithoutHyphen() {
|
|
|
return UUID.randomUUID().toString().replaceAll("-", "");
|
|
|
}
|
|
|
|
|
|
- public static String trimStart(String source, char element){
|
|
|
+ public static String trimStart(String source, char element) {
|
|
|
// source.replaceFirst()
|
|
|
boolean beginIndexFlag = true;
|
|
|
boolean endIndexFlag = true;
|
|
|
- do{
|
|
|
+ do {
|
|
|
int beginIndex = source.indexOf(element) == 0 ? 1 : 0;
|
|
|
int endIndex = source.lastIndexOf(element) + 1 == source.length() ? source.lastIndexOf(element) : source.length();
|
|
|
source = source.substring(beginIndex, endIndex);
|
|
@@ -111,6 +108,20 @@ public class StringHelper {
|
|
|
return source;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 过滤emoji表情
|
|
|
+ *
|
|
|
+ * @param source
|
|
|
+ * @return 过滤后的字符串
|
|
|
+ */
|
|
|
+ public static String filterEmoji(String source) {
|
|
|
+ if (isNullOrWhiteSpace(source)) {
|
|
|
+ return source;
|
|
|
+ }
|
|
|
+
|
|
|
+ return source.replaceAll("[\\ud800\\udc00-\\udbff\\udfff\\ud800-\\udfff]", "");
|
|
|
+ }
|
|
|
+
|
|
|
public static String toString(Object obj, String defaultValue) {
|
|
|
return obj != null ? obj.toString() : defaultValue;
|
|
|
}
|
|
@@ -120,14 +131,79 @@ public class StringHelper {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 判断字符串是否为非负整数
|
|
|
+ * 生成32位md5码
|
|
|
*
|
|
|
- * @param str
|
|
|
+ * @param text
|
|
|
* @return
|
|
|
*/
|
|
|
- public static boolean isNonNegativeInteger(String str) {
|
|
|
- Pattern pattern = Pattern.compile(NON_NEGATIVE_INTEGER_REGEX);
|
|
|
- Matcher matcher = pattern.matcher(str);
|
|
|
- return matcher.matches();
|
|
|
+ public static String md5(String text) {
|
|
|
+ StringBuilder builder = new StringBuilder();
|
|
|
+
|
|
|
+ try {
|
|
|
+ MessageDigest digest = MessageDigest.getInstance("md5");
|
|
|
+ byte[] result = digest.digest(text.getBytes());
|
|
|
+
|
|
|
+ for (byte b : result) {
|
|
|
+ int number = b & 0xff;
|
|
|
+ String str = Integer.toHexString(number);
|
|
|
+ if (str.length() == 1) {
|
|
|
+ builder.append("0");
|
|
|
+ }
|
|
|
+ builder.append(str);
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (NoSuchAlgorithmException e) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ return builder.toString();
|
|
|
}
|
|
|
+
|
|
|
+ public static String padLeft(String str, int totalWidth, char paddingChar) {
|
|
|
+ if (str == null || str.length() >= totalWidth) {
|
|
|
+ return str;
|
|
|
+ }
|
|
|
+
|
|
|
+ StringBuilder result = new StringBuilder(totalWidth);
|
|
|
+ int len = totalWidth - str.length();
|
|
|
+ for (; len > 0; len--) {
|
|
|
+ result.append(paddingChar);
|
|
|
+ }
|
|
|
+ result.append(str);
|
|
|
+// } else {
|
|
|
+// result.append(source);
|
|
|
+// for (; len > 0; len--) {
|
|
|
+// result.append(fillChar);
|
|
|
+// }
|
|
|
+// }
|
|
|
+ return result.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 超出的字符用省略号处理
|
|
|
+ *
|
|
|
+ * @param str 字符串
|
|
|
+ * @param num 处理长度(超过这个长度之后才进行处理,并且最后三个长度用 '...' 来替代)
|
|
|
+ * @return 处理后的字符串
|
|
|
+ */
|
|
|
+ public static String dealStringWithEllipsis(String str, int num) {
|
|
|
+ if (Checker.isNone(str) || str.trim().length() == 0) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ if (str.length() > num) {
|
|
|
+ str = str.substring(0, num-3).concat("...");
|
|
|
+ }
|
|
|
+ return str;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 超出的字符用省略号处理(长度超过20后进行处理,并且最后三个长度用 '...' 来替代)
|
|
|
+ *
|
|
|
+ * @param str 字符串
|
|
|
+ * @return 处理后的字符串
|
|
|
+ */
|
|
|
+ public static String dealStringWithEllipsis(String str) {
|
|
|
+ return dealStringWithEllipsis(str,20);
|
|
|
+ }
|
|
|
+
|
|
|
}
|