英文:
Split String if it contains a word from the List
问题
我逐行读取文本文件,并且想要在每行中包含特定列表中的单词时对其进行分割。
示例:
String str1 = BuraBua是我居住的地方。
String str2 = 纽约是我居住的地方。
现在我有一个字符串列表:List li = Arrays.asList("BuraBura", "纽约");
我想要类似这样的效果:
String[] strList = str1.split(// 如果str1包含来自li的单词,则基于该单词进行分割);
有什么建议吗?
英文:
I am reading a text file line by line and I want to split each line if they contain a word that is present in a specific List.
Example:
String str1 = BuraBua is the Place I live in.
String str2 = New York is the Place I live.
Now I have a List of String like : List li = Arrays.asList("BuraBura", "New York");
I want something like:
String[] strList = str1.split(// if str1 contains a word from li then split on that basis);
Any suggestions??
答案1
得分: 0
这有点简单。您可以通过迭代白名单/关键字来实现。
Java 8及更高版本:
不需要使用`filters`、`maps`和`collectors`。这是多余的,因为您将获得列表的列表。最好使用简单的`forEach`流,通过迭代键来实现。
方法:
private final List<String> keys = Arrays.asList("apple", "orange");
public List<String> getSplitted(String text) {
List<String> parts = new ArrayList<>();
keys.forEach(key -> {
if (text.toLowerCase().contains(key.toLowerCase()))
parts.addAll(Arrays.asList(text.split(key)));
});
return parts;
}
旧版Java:
使用简单的`for`迭代器
方法:
private final List<String> keys = Arrays.asList("apple", "orange");
public List<String> getSplitted(String text) {
List<String> parts = new ArrayList<>();
for (String key : keys) {
if (text.toLowerCase().contains(key.toLowerCase()))
parts.addAll(Arrays.asList(text.split(key)));
}
return parts;
}
测试(Groovy):
def "Splitter test"() { given: def text = "I like orange very much. Because orange is very delicious" when: def result = new Splitter().getSplitted(text) then: result == ["I like ", " very much. Because ", " is very delicious"] }
如果您的需求有所不同,请进行评论。
英文:
This is kind a simple. You can do it by iterating whitelist/keys.
<h3>Java 8 and later:</h3>
Don't need to use filters
, maps
and collectors
. It's redundant, because you will get list of lists. Better to use simple forEach
stream by iterating keys.
Method:
private final List<String> keys = Arrays.asList("apple", "orange");
public List<String> getSplitted(String text) {
List<String> parts = new ArrayList<>();
keys.forEach(key -> {
if (text.toLowerCase().contains(key.toLowerCase()))
parts.addAll(Arrays.asList(text.split(key)));
});
return parts;
}
<h3>Older Java:</h3>
Use simple for
iterator
Method
private final List<String> keys = Arrays.asList("apple", "orange");
public List<String> getSplitted(String text) {
List<String> parts = new ArrayList<>();
for (String key : keys) {
if (text.toLowerCase().contains(key.toLowerCase()))
parts.addAll(Arrays.asList(text.split(key)));
}
return parts;
}
<h3>Test (groovy):</h3>
> groovy
>def "Splitter test"() {
> given:
> def text = "I like orange very much. Because orange is very delicious"
> when:
> def result = new Splitter().getSplitted(text)
> then:
> result == ["I like ", " very much. Because ", " is very delicious"]
>}
>
Comment if your needs is different.
答案2
得分: -1
List<List<String>> lo = li.stream()
.filter(str -> str1.contains(str))
.map(str -> { return Arrays.asList(str1.split(str)); })
.collect(Collectors.toList());
注:如果您的输入包含多个分割字符串,则需要进一步修改。如果出现这种情况,您可以尝试使用for循环。希望能够提供一些思路。谢谢
英文:
List<List<String>> lo = li.stream()
.filter(str -> str1.contains(str))
.map(str -> { return Arrays.asList(str1.split(str)); })
.collect(Collectors.toList());
Note: If your input contains multiple split string then this has to be further modified. In case that happens to be the case, then you can try for loop. Hope it gives some path. Thanks
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论