0

I want to split sentence that has +
I code this:

String[]s;
String a;
s=a.split("+");

but it throw this exeption:
Exception in thread "main" java.util.regex.PatternSyntaxException: Dangling meta character '+' near index 0

what should I do??
thanks

2
Contributors
2
Replies
13
Views
3 Years
Discussion Span
Last Post by sGirl123
0

The parameter for split is a REGEX. In a REGEX the + character is a special character. To use it as a literal + you need to "escape" it with a single \ . To enter a single \ in a Java String you need to code a double \ (yes, messy isn't it). In summary: to split on + characaters you need to code split("\\+")
Check out the doc for java.util.regex.Pattern or any REGEX tutorial.

Edited by JamesCherrill

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.