Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

split("\\") and error

Tags:

String str = "\u0054\u0068\u0069\u006e\u006b\u0050\u0061\u0064";  String[] strArray = str.split("\\");  

but this error occured.

Exception in thread "main" java.util.regex.PatternSyntaxException: Unexpected internal error near index 1

like image 580
evilYoung Avatar asked Nov 01 '12 10:11

evilYoung


People also ask

What does split \\ do in Java?

Java split() function is used to splitting the string into the string array based on the regular expression or the given delimiter. The resultant object is an array contains the split strings.

What is the use of \\ s in string?

\\s - matches single whitespace character. \\s+ - matches sequence of one or more whitespace characters.

Is split or regex faster?

split is faster, but complex separators which might involve look ahead, Regex is only option.

Are split is not a function?

The "split is not a function" error occurs when we call the split() method on a value that is not of type string. To solve the error, convert the value to a string by using the toString() method before you call split() , or make sure to only call the split method on strings. Here is an example of how the error occurs.


1 Answers

it should be

String[] strArray = str.split("\\\\"); 

the reason why is because in Regex, \ has special meaning so you need to escape it into \\.

and in java, \\ should be equal to "\\\\"

like image 57
John Woo Avatar answered Oct 11 '22 10:10

John Woo