Java - 如何在加号上拆分字符串? [英] Java - How to split a string on plus signs?
本文介绍了Java - 如何在加号上拆分字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图在加号上分割算术表达式(例如1 + 2 + 10 + 15)。但是,我没有设法编写适当的正则表达式。我认为这样可行:
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:
expression.split("\\+");
但不是。你知道正确的解决方案吗?
but it doesn't. Do you know the correct solution?
推荐答案
确实如此。但是 split(...)
返回一个数组,它不会将 String
转换为字符串[]
。试试这个:
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("\\+");
这篇关于Java - 如何在加号上拆分字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文