删除特定字符后的所有内容(IndexOfAny 和删除)
我有以下字符串数组 (quantityInForPriceBandPopUp[3]) 数据:
10 - 24
25 - 99
100 - 249
5000+
在 C# 中,如果我将此数组放入此:
quantityInForPriceBandPopUp[i] = quantityInForPriceBandPopUp[i].Remove(quantityInForPriceBandPopUp[i].IndexOfAny(new char[] { ' ', '+' }));
我得到:
10
25
100
5000
如何在 Java 中达到相同的结果?理想情况下,我正在寻找一行答案。如果不可能,那么最短的也可以。
I have the following string array (quantityInForPriceBandPopUp[3]) data:
10 - 24
25 - 99
100 - 249
5000+
In C#, if I put this array through this:
quantityInForPriceBandPopUp[i] = quantityInForPriceBandPopUp[i].Remove(quantityInForPriceBandPopUp[i].IndexOfAny(new char[] { ' ', '+' }));
I get this:
10
25
100
5000
How do I reach the same result in Java? Ideally I am looking for a one line answer. If it is impossible, then the shortest would work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
没有有效的一行答案,因为
java.lang.String
API 中没有与indexOfAny
直接等效的函数。这是几行有效的等价物。
您可以轻松地将其转换为静态辅助方法。
There is no efficient one-line answer, because there is no direct equivalent to
indexOfAny
in thejava.lang.String
API.Here's an efficient equivalent in a couple of lines.
And you could easily turn that into a static helper method.
看看
http://commons.apache.org/lang /api-2.6/org/apache/commons/lang/StringUtils.html
它有indexOfAny方法和更多字符串方法
Have a look at
http://commons.apache.org/lang/api-2.6/org/apache/commons/lang/StringUtils.html
It has the indexOfAny method and many more string methods
也许不是最有效的,但您可以使用正则表达式和 replaceFirst
基本上,它找到空格
' '
或加号+< 的第一个实例/code>,我们必须用两个
\
进行转义,因为+
以及其后面的任何其他字符也是正则表达式中的特殊符号.*< /code> 到末尾
$
,并将其替换为空字符串。如果您需要扩展代码以捕获其他分隔符,例如
.
,您只需使用另一个|
运算符将分隔符添加到组中:现在,这将编译一个新的正则表达式 Pattern 每个循环,如果数组中有很多字符串,这绝对不理想。在这种情况下,您可能会考虑首先在循环之外编译一个单独的模式:
如果您之后想要这些部分,则可以使用像这样的正则表达式:
注意,如果您仍然想捕获“5000”的“,则需要修改它” 5000+”。
可能值得您花时间制作一个单独的通用正则表达式,使用“\d+”来定位数字,例如:
那么只需使用匹配器和 Matcher.group 来挑选特定的数字。
相关链接:
字符串文档< /a>
模式文档
匹配器文档
Java 中的正则表达式
正则表达式维基百科
Maybe not the most efficient, but you could use regular expressions and replaceFirst
Basically, it finds the first instance of either a space
' '
or plus sign+
, which we had to escape with two\
since+
is also a special symbol in regular expressions, along with any other characters following it.*
up to the end$
, and replaces it with an empty string.If you needed to extend the code to catch other delimiters, like maybe
.
, you just add the delimiter to the group with another|
operator:Now, this will compile a new regex Pattern each loop, which is definitely not ideal if you have a lot of Strings in your array. In that case, you might consider compiling a separate pattern first outside your loop:
If you want the parts afterwards, a regular expression like this would do:
Note, this would need to be modified if you still want to capture "5000" for "5000+".
It may be worth your while to make a separate generic regex that uses "\d+" to locate numbers, like:
Then it's just a matter of using a Matcher and Matcher.group to pick out specific numbers.
Related links:
String documentation
Pattern documentation
Matcher documentation
Regular Expressions in Java
Wikipedia on Regular Expressions