|
@@ -1,10 +1,18 @@
|
|
|
package com.gis.common.util;
|
|
|
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.junit.Test;
|
|
|
+
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.HashSet;
|
|
|
+import java.util.List;
|
|
|
import java.util.Set;
|
|
|
|
|
|
/**
|
|
|
* Created by owen on 2021/10/18 0018 17:35
|
|
|
*/
|
|
|
+@Slf4j
|
|
|
public class MyStrUtil {
|
|
|
|
|
|
|
|
@@ -15,11 +23,38 @@ public class MyStrUtil {
|
|
|
* @return
|
|
|
*/
|
|
|
public static String getFilterMsg(Set<Object> filterKey, String content){
|
|
|
+// String a = content;
|
|
|
for (Object s : filterKey) {
|
|
|
if (s != null){
|
|
|
- content = content.replaceAll(s.toString(), "**");
|
|
|
+ String regex = s.toString();
|
|
|
+ if (".".equals(regex) || "*".equals(regex)){ // 处理特殊字符
|
|
|
+ regex = regex.replace(regex,"");
|
|
|
+ if (StrUtil.isBlank(regex)){
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ }
|
|
|
+// content = content.replaceAll(regex, "**");
|
|
|
+ content = content.replace(regex, "**");
|
|
|
}
|
|
|
}
|
|
|
+// log.info(a + ": {}", content );
|
|
|
return content;
|
|
|
}
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void test(){
|
|
|
+ Set<Object> set = new HashSet<>();
|
|
|
+ set.add(".");
|
|
|
+ set.add("0.0");
|
|
|
+ set.add(".1");
|
|
|
+ set.add("0.1");
|
|
|
+ set.add("2.1");
|
|
|
+ set.add("*");
|
|
|
+ List<String> asList = Arrays.asList("我是....点点点", "我是.点", "我是0.0零点零", "我是.1点一", "我是0107",
|
|
|
+ "我是0.1零点一,我是0.1零点一", "我是2.1二点一,我是2.1二点一", "我是***星星");
|
|
|
+ for (String s : asList) {
|
|
|
+ log.info(s + ": {}", getFilterMsg(set, s) );
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
}
|