Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java and string split

Tags:

java

split this String using function split. Here is my code:

String data= "data^data";
String[] spli = data.split("^");

When I try to do that in spli contain only one string. It seems like java dont see "^" in splitting. Do anyone know how can I split this string by letter "^"?

EDIT

SOLVED :P

like image 541
klemens Avatar asked Dec 04 '22 05:12

klemens


1 Answers

This is because String.split takes a regular expression, not a literal string. You have to escape the ^ as it has a different meaning in regex (anchor at the start of a string). So the split would actually be done before the first character, giving you the complete string back unaltered.

You escape a regular expression metacharacter with \, which has to be \\ in Java strings, so

data.split("\\^")

should work.

like image 179
Joey Avatar answered Dec 17 '22 16:12

Joey