我想过滤字符串列表但忽略正则表达式。例如:查找“test.xy”应仅显示“test.xy”或“abctest.xy”等条目,而不显示“testaxy”等条目。我不想要“。”作为通配符。
我怎样才能做到这一点?
最佳答案
如果您想过滤字符串并将它们收集到一个新列表中,您可以按如下方式操作;
List<String> strings = List.of("mytest.xy",
"abctest.xy", "test.xy", "testaxy", "testy");
String target = "test.xy";
List<String> result = strings.stream()
.filter(str -> str.contains(target))
.collect(Collectors.toList());
result.forEach(System.out::println);
打印
mytest.xy
abctest.xy
test.xy
关于java - 字符串过滤器忽略正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59742258/