如何使用Java流获取字符串的特定部分?

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

How to get special part of string using java streams?

问题

我有一个名为abbreviations.txt的文件,其中有特殊信息:

示例 abbreviations.txt 内容:

PCAP_Personal Computer_Apple
NBHP_NoteBook_Hewlett Packard
TVSG_Televisor_Samsung

我需要将所有品牌名称放入一个新的字符串列表中。我尝试使用以下代码:

Stream<String> abbreviations = Files.lines(Paths.get("src/main/resources/raceData/abbreviations.txt"))
                .flatMap(Pattern.compile("_")::splitAsStream);
List<String> dates = abbreviations.collect(Collectors.toList());
dates.forEach(System.out::println);

但是作为列表返回的结果是:

PCAP_Personal Computer
Apple
TVSG_Televisor
Samsung
NBHP_NoteBook
Hewlett Packard
英文:

I have file abbreviations.txt where I have special infos:

Example abbreviations.txt :

PCAP_Personal Computer_Apple
NBHP_NoteBook_Hewlett Packard
TVSG_Televisor_Samsung

and I need to get all brand names into new List String. Im trying use this:

 Stream&lt;String&gt; abbreviations = Files.lines(Paths.get(&quot;src/main/resources/raceData/abbreviations.txt&quot;))
            .flatMap(Pattern.compile(&quot;_&quot;)::splitAsStream);
    List&lt;String&gt; dates = abbreviations.collect(Collectors.toList());
    dates.forEach(System.out::println);

But as List I get:

PCAP_Personal Computer
Apple
TVSG_Televisor
Samsung
NBHP_NoteBook
Hewlett Packard

答案1

得分: 5

splitAsStream

不必要的。只需使用:

.map(line -> line.split("_")[2])

当然,这假设所有行都具有正确的格式。你当然可以过滤掉格式错误的行,例如使用:

.map(line -> line.split("_"))
.filter(parts -> parts.length >= 3)
.map(parts -> parts[2])

然后,你可以将其放入数组中,使用 toArray。然而,我建议使用 List,因为绝大多数实现要强大得多。你可以使用 Collector 进行操作:

.collect(Collectors.toList());
英文:

splitAsStream?

That's not necessary. Just use

.map(line -&gt; line.split(&quot;_&quot;)[2])

This of course assumes that all lines have the correct format. You could of course filter out malformed lines, for example, using

.map(line -&gt; line.split(&quot;_&quot;))
.filter(parts -&gt; parts.length &gt;= 3)
.map(parts -&gt; parts[2])

You could then put it into an array, using toArray. However, I recommend to use a List instead, because by far the most implementations are way more powerful. You can do so using a Collector:

.collect(Collectors.toList());

答案2

得分: 1

如果品牌名称是在最后一个&#39;_&#39;之后的话,

String s = "PCAP_Personal Computer_Apple\n" +
            "NBHP_NoteBook_Hewlett Packard\n" +
            "TVSG_Televisor_Samsung" ;

Stream<String> abbreviations = Arrays.stream(s.split("\\n"));
List<String> brandNames  =  abbreviations
        .map(abbr -> abbr.substring(abbr.lastIndexOf('_') + 1))
        .collect(Collectors.toList());

brandNames.forEach(System.out::println);

将会打印出:

Apple
Hewlett Packard
Samsung

如果你需要它们作为一个数组,只需使用:

String[] brandNames  =  abbreviations
    .map(abbr -> abbr.substring(abbr.lastIndexOf('_') + 1))
    .toArray(String[]::new);
英文:

If the brand names are what is after the last &#39;_&#39;

    String s = &quot;PCAP_Personal Computer_Apple\n&quot; +
            &quot;NBHP_NoteBook_Hewlett Packard\n&quot; +
            &quot;TVSG_Televisor_Samsung&quot; ;

    Stream&lt;String&gt; abbreviations = Arrays.stream(s.split(&quot;\\n&quot;));
    List&lt;String&gt; brandNames  =  abbreviations
            .map(abbr -&gt; abbr.substring(abbr.lastIndexOf(&#39;_&#39;) + 1))
            .collect(Collectors.toList());

    brandNames.forEach(System.out::println);

will print you

Apple
Hewlett Packard
Samsung

If you need them to be an array, just use

            String[]  brandNames  =  abbreviations
                .map(abbr -&gt; abbr.substring(abbr.lastIndexOf(&#39;_&#39;) + 1))
                .toArray(String[]::new);

答案3

得分: 0

尝试一下。它只会删除从最后一个下划线开始的所有内容。

String[] lines = { "PCAP_Personal Computer_Apple",
        "NBHP_NoteBook_Hewlett Packard",
        "TVSG_Televisor_Samsung",
        "And_more_than_two_underscores_IBM" };

List<String> vendors = Arrays.stream(lines)
        .map(str -> str.replaceAll(".*(?<=_)", ""))
        .collect(Collectors.toList());
vendors.forEach(System.out::println);

输出结果

Apple
Hewlett Packard
Samsung
IBM
英文:

Try this. It just deletes everything up to and including the last underscore.

String[] lines = { &quot;PCAP_Personal Computer_Apple&quot;,
		&quot;NBHP_NoteBook_Hewlett Packard&quot;,
		&quot;TVSG_Televisor_Samsung&quot;,
		&quot;And_more_than_two_underscores_IBM&quot; };

List&lt;String&gt; vendors = Arrays.stream(lines)
		.map(str -&gt; str.replaceAll(&quot;.*(?&gt;_)&quot;, &quot;&quot;))
		.collect(Collectors.toList());
vendors.forEach(System.out::println);

Prints

Apple
Hewlett Packard
Samsung
IBM


</details>



huangapple
  • 本文由 发表于 2020年10月16日 01:03:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/64376484.html
匿名

发表评论

匿名网友

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

确定