Java StringUtils 根据逗号和空格拆分
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18838764/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
StringUtils to split based on comma and space
提问by Adam
Could you please let me know whether we have any StringUtils function to split based on comma and space. Basically i am wondering whether we have any function which will split based on two delimiters. I have written custom function to do the same, but just checking whether we have any good function in any utils package.
您能否让我知道我们是否有任何 StringUtils 函数可以根据逗号和空格进行拆分。基本上我想知道我们是否有任何基于两个分隔符拆分的函数。我已经编写了自定义函数来做同样的事情,但只是检查我们在任何 utils 包中是否有任何好的函数。
The way I have done is to replace first delimiter with second one and then split based on second delimiter.
我所做的方法是用第二个分隔符替换第一个分隔符,然后根据第二个分隔符进行拆分。
回答by Reimeus
回答by shmosel
String.split()
accepts a regex expression, so you can use:
String.split()
接受正则表达式,因此您可以使用:
"test,string split".split("[, ]")
EDIT: Just noticed Reimeus already mentioned this.
编辑:刚刚注意到 Reimeus 已经提到了这一点。