从列表中提取包含特定单词的部分并分割字符串。

huangapple go评论115阅读模式
英文:

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&lt;String&gt; keys = Arrays.asList(&quot;apple&quot;, &quot;orange&quot;);

public List&lt;String&gt; getSplitted(String text) {
    List&lt;String&gt; parts = new ArrayList&lt;&gt;();
    keys.forEach(key -&gt; {
       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&lt;String&gt; keys = Arrays.asList(&quot;apple&quot;, &quot;orange&quot;);

public List&lt;String&gt; getSplitted(String text) {
    List&lt;String&gt; parts = new ArrayList&lt;&gt;();
    for (String key : keys) {
        if (text.toLowerCase().contains(key.toLowerCase()))
            parts.addAll(Arrays.asList(text.split(key)));
    }
    return parts;
}

<h3>Test (groovy):</h3>

> groovy
&gt;def &quot;Splitter test&quot;() {
&gt; given:
&gt; def text = &quot;I like orange very much. Because orange is very delicious&quot;
&gt; when:
&gt; def result = new Splitter().getSplitted(text)
&gt; then:
&gt; result == [&quot;I like &quot;, &quot; very much. Because &quot;, &quot; is very delicious&quot;]
&gt;}
&gt;

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&lt;List&lt;String&gt;&gt; lo = li.stream()
    .filter(str -&gt; str1.contains(str))
    .map(str -&gt; { 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

huangapple
  • 本文由 发表于 2020年10月27日 13:20:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/64548680.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定