java拆分字符串,如果它包含列表中的一个单词
我正在逐行阅读一个文本文件,如果它们包含特定列表中的单词,我想将每一行拆分
例如:
String str1 = BuraBua is the Place I live in.
String str2 = New York is the Place I live.
现在我有一个字符串列表,比如:List li = Arrays.asList("BuraBura", "New York");
我想要这样的东西:
String[] strList = str1.split(// if str1 contains a word from li then split on that basis);
有什么建议吗
# 1 楼答案
这是一种简单的方法。可以通过迭代白名单/键来实现
Java 8及更高版本:
不需要使用“过滤器”、“地图”和“收集器”。这是多余的,因为你会得到一个列表。最好通过迭代键来使用简单的“forEach”流。方法:
旧版Java:
使用simple`for`迭代器方法
测试(groovy):
如果您的需求不同,请发表评论