增加过滤字符串的方法

This commit is contained in:
lixiaohua
2020-08-01 21:09:40 +08:00
parent 02b0f6194f
commit 31b1902d6b
2 changed files with 35 additions and 14 deletions

View File

@@ -22,6 +22,7 @@ import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.function.Predicate;
import java.util.regex.Pattern;
/**
@@ -1332,20 +1333,7 @@ public class StrUtil {
* @return 清理后的字符串
*/
public static String cleanBlank(CharSequence str) {
if (str == null) {
return null;
}
int len = str.length();
final StringBuilder sb = new StringBuilder(len);
char c;
for (int i = 0; i < len; i++) {
c = str.charAt(i);
if (false == CharUtil.isBlankChar(c)) {
sb.append(c);
}
}
return sb.toString();
return filter(str, c -> !CharUtil.isBlankChar(c));
}
// ------------------------------------------------------------------------------ Split
@@ -4324,4 +4312,29 @@ public class StrUtil {
}
return new String(buffer);
}
/**
* 过滤字符串
*
* @param str 字符串
* @param predicate 过滤条件
* @return 过滤后的字符串
* @since 5.4.0
*/
public static String filter(CharSequence str, Predicate<Character> predicate) {
if (str == null || predicate == null) {
return str(str);
}
int len = str.length();
final StringBuilder sb = new StringBuilder(len);
char c;
for (int i = 0; i < len; i++) {
c = str.charAt(i);
if (predicate.test(c)) {
sb.append(c);
}
}
return sb.toString();
}
}

View File

@@ -456,4 +456,12 @@ public class StrUtilTest {
String brief = StrUtil.brief(str, maxLength);
Assert.assertEquals(brief.length(), maxLength);
}
@Test
public void filterTest() {
final String filterNumber = StrUtil.filter("hutool678", CharUtil::isNumber);
Assert.assertEquals(filterNumber, "678");
String cleanBlank = StrUtil.filter(" 你 好 ", c -> !CharUtil.isBlankChar(c));
Assert.assertEquals("你好", cleanBlank);
}
}