views:

285

answers:

3

Hi! 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?

+10  A: 

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("\\+");
Bart Kiers
Thanks! It really does. It's funny though because I've tried it again in an empty project and it worked. However, it throws an exception in my other project. I'll try to look into it.
John Manak
If it still doesn't work, don't forget to post the exception :)
Rich
Good to hear it. I bet there's something different in the other project! :)
Bart Kiers
A: 

I was having the same trouble on a laptop (without a number pad) and had to use the function (Fn) key to type in the + sign.

stewartmathman
A: 

this way

expression.split("[+]");

mikail