How to escape + character while using split function call in java?
split declaration
String[] split(String regularExpression)
thats what i did
services.split("+"); //says dongling metacharacter
services.split("\+"); //illegal escape character in string literal
But it allows to do something like this
String regExpr="+";
So in a character literal, you need to escape single quotes, e.g. '\'' . So all you need is "\\'" , escaping only the backslash.
Escape CharactersUse the backslash character to escape a single character or symbol.
These are escape characters which are used to manipulate string. \t Insert a tab in the text at this point. \b Insert a backspace in the text at this point. \n Insert a newline in the text at this point.
Since the +
is a regex meta-character (denoting an occurrence of 1 or more times), you will have to escape it with \
(which also has to be escaped because it's a meta-character that's being used when describing the tab character, the new line character(s) \r\n
and others), so you have to do:
services.split("\\+");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With