英文:
How to delete item in arraylist contains number and other elements
问题
我有一个ArrayList,里面有许多元素。
每个元素都包含一组整数数字。
最终,我有一些电话号码。
有时号码中会带有字母或代码。
任何带有带有某些符号或字母的数字的项目我都想从ArrayList中移除。
// 我的代码:
ArrayList
Numsphoon.add("966556160");
Numsphoon.add("96659336.");
Numsphoon.add("966560809");
Numsphoon.add("966556160");
Numsphoon.add("966556160");
英文:
I have an arraylist It's got a bunch of elements inside.
Every element is in it it contains a set of integer numbers.
Eventually, I have a number of phone numbers.
Sometimes there's a number with a letter or code in it.
Any item with numbers with some symbols or letters I want to remove from the arraylist.
// my code :
ArrayList<String> Numsphoon = new ArrayList<>();
Numsphoon.add("966556160");
Numsphoon.add("96659336.");
Numsphoon.add("966560809");
Numsphoon.add("966556160");
Numsphoon.add("966556160");
答案1
得分: 5
Numspoon.removeIf(str -> !str.matches("[0-9]+"));
英文:
Numsphoon.removeIf(str -> !str.matches("[0-9]+"));
答案2
得分: 0
由 Turamarth 的回答 是正确的。我编写这个答案的唯一原因是如果第一个数字需要是 1-9
,其余数字可以是 0-9
,那么正则表达式将是 [1-9][0-9]+
在这种情况下,修改后的解决方案(由 Turamarth)将是
Numsphoon.removeIf(str -> !str.matches("[1-9][0-9]+"));
英文:
The answer by Turamarth is spot on. The only thing for which I am writing this answer is if the first digit needs to be 1-9
and rest of the digits can be 0-9
, the regex will be [1-9][0-9]+
In that case, the modified solution (by Turamarth) will be
Numsphoon.removeIf(str -> !str.matches("[1-9][0-9]+"));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论