Java - 如何在加号上拆分字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2198373/
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
Java - How to split a string on plus signs?
提问by John Manak
I was trying to split an arithmetic expression (eg "1+2+10+15") on the plus signs. However, I didn't manage to write the appropriate regular expression. I thought this would work:
我试图在加号上拆分算术表达式(例如“1+2+10+15”)。但是,我没有设法编写适当的正则表达式。我认为这会奏效:
expression.split("\+");
but it doesn't. Do you know the correct solution?
但事实并非如此。你知道正确的解决方法吗?
采纳答案by Bart Kiers
It does. However split(...)
returns an array, it does not "transform" your String
into a String[]
. Try this:
确实如此。但是split(...)
返回一个数组,它不会将您的“转换”String
为String[]
. 尝试这个:
String expression = "1+2+10+1";
String[] tokens = expression.split("\+");
回答by mikail
this way
这边走
expression.split("[+]");