如何从包含数字和其他元素的数组列表中删除项目

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

How to delete item in arraylist contains number and other elements

问题

我有一个ArrayList,里面有许多元素。
每个元素都包含一组整数数字。
最终,我有一些电话号码。
有时号码中会带有字母或代码。
任何带有带有某些符号或字母的数字的项目我都想从ArrayList中移除。

// 我的代码:

ArrayList Numsphoon = new 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&lt;String&gt; Numsphoon = new ArrayList&lt;&gt;();
      
      Numsphoon.add(&quot;966556160&quot;);
      Numsphoon.add(&quot;96659336.&quot;);
      Numsphoon.add(&quot;966560809&quot;);
      Numsphoon.add(&quot;966556160&quot;);
      Numsphoon.add(&quot;966556160&quot;);

答案1

得分: 5

Numspoon.removeIf(str -> !str.matches("[0-9]+"));

英文:
Numsphoon.removeIf(str -&gt; !str.matches(&quot;[0-9]+&quot;));

答案2

得分: 0

由 Turamarth 的回答 是正确的。我编写这个答案的唯一原因是如果第一个数字需要是 1-9,其余数字可以是 0-9,那么正则表达式将是 [1-9][0-9]+

在这种情况下,修改后的解决方案(由 Turamarth)将是

Numsphoon.removeIf(str -&gt; !str.matches(&quot;[1-9][0-9]+&quot;));
英文:

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 -&gt; !str.matches(&quot;[1-9][0-9]+&quot;));

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

发表评论

匿名网友

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

确定