从字符串的ArrayList中删除数字

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

Remove numbers from ArrayList of Strings

问题

我有一个字符串的ArrayList,我想从中删除数字。如何实现?

ArrayList<String> arraylist = new ArrayList();

arraylist.add("01 Hello 88");
arraylist.add("02 World 88");

例如,我只想删除 "01" 和 "02",其余部分保持不变。

英文:

I have ArrayList of Strings and I want to remove numbers from it. How Can achieve it?

ArrayList&lt;String&gt; arraylist = new ArrayList();

arraylist.add(&quot;01 Hello 88&quot;);
arraylist.add(&quot;02 World 88&quot;);

For example I just want to remove 01 and 02 .. and rest can be same

答案1

得分: 0

你似乎只想要子字符串,因为你只想去掉01而不是88,那么你可以使用substring()

for (int i = 0; i < arraylist.size(); i++) {
  arraylist.set(i, arraylist.get(i).substring(3));
}

或者你可以使用Stream API

List<String> arraylist = new ArrayList();
arraylist.add("01 Hello 88");
arraylist.add("02 World 88");
arraylist = arraylist.stream().map(a -> a.substring(3)).collect(Collectors.toList());
英文:

Seems like you want subString only since you want to remove only 01 not 88 then you can use substring()

for (int i = 0; i &lt; arraylist.size(); i++) {
  arraylist.set(i, arraylist.get(i).substring(3));
}

Or You can use Stream API

List&lt;String&gt; arraylist = new ArrayList();
arraylist.add(&quot;01 Hello 88&quot;);
arraylist.add(&quot;02 World 88&quot;);
arraylist = arraylist.stream().map(a -&gt; a.substring(3)).collect(Collectors.toList());

答案2

得分: 0

有趣的问题。以下是代码部分的翻译:

UnaryOperator<List<String>> trimHeader = (inList) -> {
    List<String> outList = new ArrayList<>();
    for (String s : inList) {
        String[] elements = s.split(" ");
        outList.add(Arrays.stream(elements).skip(1).collect(Collectors.joining(" ")));
    }
    return outList;
};

List<String> list = new ArrayList<>();
list.add("01 Hello 88");
list.add("02 World 88");
trimHeader.apply(list).forEach(System.out::println);

输出:

Hello 88
World 88

请注意,Lambda函数对原始列表没有副作用。

[编辑]
以上更新了一个变量名。
另外,考虑使用此回复中提供的函数以实现更通用的用途——它基于对空格的检查来移除第一个元素。第一个元素可以是数字或一些字符。

英文:

Interesting question. Here it is:

UnaryOperator&lt;List&lt;String&gt;&gt; trimHeader = (inList) -&gt; {
    List&lt;String&gt; outList = new ArrayList&lt;&gt;();
    for (String s : inList) {
        String[] elements = s.split(&quot; &quot;);
        outList.add(Arrays.stream(elements).skip(1).collect(Collectors.joining(&quot; &quot;)));
    }
    return outList;
};

List&lt;String&gt; list = new ArrayList&lt;&gt;();
list.add(&quot;01 Hello 88&quot;);
list.add(&quot;02 World 88&quot;);
trimHeader.apply(list).forEach(System.out::println);

Output:

Hello 88<br>
World 88

Notice that the Lambda function brings no side-effect to the original list.

[Edit]
Updated a variable name above.
In addition, consider the function provided in this reply for a more general purpose usage -- it removes the first element based on checking the space. The first element can be a number or some characters.

答案3

得分: 0

以下是翻译好的内容:

删除以数字开头并跟随空格的最简单方法是使用正则表达式进行匹配,然后将其替换为空字符串。

正则表达式应为 ^\d+\s+,在Java字符串字面值中为 "^\\d+\\s+"

要在Java 8+中更新列表中的所有值,可以使用 replaceAll(UnaryOperator<E> operator) 方法,如下所示:

ArrayList<String> arraylist = new ArrayList<>();
arraylist.add("01 Hello 88");
arraylist.add("02 World 88");

arraylist.replaceAll(s -> s.replaceFirst("^\\d+\\s+", ""));

System.out.println(arraylist);

输出

[Hello 88, World 88]
英文:

The easiest way to remove leading digits followed by spaces, is to use a regular expression to match that, and then replace it with an empty string.

The regex would be ^\d+\s+, which as a Java string literal would be &quot;^\\d+\\s+&quot;.

To update all values in a list in Java 8+, you can do it using the replaceAll(UnaryOperator&lt;E&gt; operator) method, like this:

ArrayList&lt;String&gt; arraylist = new ArrayList&lt;&gt;();
arraylist.add(&quot;01 Hello 88&quot;);
arraylist.add(&quot;02 World 88&quot;);

arraylist.replaceAll(s -&gt; s.replaceFirst(&quot;^\\d+\\s+&quot;, &quot;&quot;));

System.out.println(arraylist);

Output

[Hello 88, World 88]

huangapple
  • 本文由 发表于 2020年8月27日 02:07:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/63603385.html
匿名

发表评论

匿名网友

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

确定