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

Recommended Answers

All 2 Replies

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.

thank you very much

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.