Separating Java and Strings

split this line using the split function. Here is my code:

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

When I try to do this, spli contains only one line. It seems that java does not see "^" in the splitting. Does anyone know how I can break this line with the letter "^"?

EDIT

SOLVED: P

+4
source share
5 answers

This is because String.split accepts a regular expression, not a literal string. You must exit ^ , since it has a different meaning in the regular expression (anchor at the beginning of the line). This way, the split will actually be done before the first character, giving you the full line back unchanged.

You avoid the regular expression metacharacter with \ , which should be \\ in Java strings, so

 data.split("\\^") 

must work.

+7
source

You need to get away from him because he takes reg-ex

 \\^ 
+3
source

Special characters such as ^ must be escaped with \

+3
source

This does not work because .split() expects its argument to be a regular expression. "^" has a special regex binding and therefore does not work as you expect. To make it work, you need to avoid this. Use \\^ .

+2
source

The reason is that the split parameter is a regular expression , so “^” means the beginning of the line. Therefore, you need to escape to ASCII- ^: use the " \\^ " parameter.

+2
source

Source: https://habr.com/ru/post/1393491/


All Articles