开发者

Java - How to split a string on plus signs?

开发者 https://www.devze.com 2022-12-19 07:29 出处:网络
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 r开发者_Python百科egular expression. I thought this would work:

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 r开发者_Python百科egular expression. I thought this would work:

expression.split("\\+");

but it doesn't. Do you know the correct solution?


It does. However split(...) returns an array, it does not "transform" your String into a String[]. Try this:

String expression = "1+2+10+1";
String[] tokens = expression.split("\\+");


this way

expression.split("[+]");
0

精彩评论

暂无评论...
验证码 换一张
取 消