英文:
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<String> arraylist = new ArrayList();
arraylist.add("01 Hello 88");
arraylist.add("02 World 88");
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 < arraylist.size(); i++) {
arraylist.set(i, arraylist.get(i).substring(3));
}
Or You can use 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());
答案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<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);
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 "^\\d+\\s+"
.
To update all values in a list in Java 8+, you can do it using the replaceAll(UnaryOperator<E> operator)
method, like this:
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);
Output
[Hello 88, World 88]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论