2

I have a string a follows

String caret= "Y^LEAD_PROJECT_V.vw^View LEAD_PROJECT_V.vw^INF^View^Extension^RECOMPILE^Unit Test^Recompile - Test ^Test View PROJECT_V.vw^^SIMPLE^^^^^^^";

I want to split this string with ^. So I used the following code to split it

String[] split = caret.split("\\^");
System.out.println(split.length);

split.length obtained :

12

split.length required:

19 // as there are 18 caret symbols in the string

I don't understand what am I doing wrong in it. Whatever regex I use, I'm getting the same result. Also I cannot modify the string caret.

Can anyone please help me in getting 19 as array length.

Micha Wiedenmann
  • 19,979
  • 21
  • 92
  • 137
Harshita Sethi
  • 2,035
  • 3
  • 24
  • 46

1 Answers1

3

Try to use "\\^",-1 like this:

  String[] split = caret.split("\\^",-1);

With using -1 you can include empty string, having an output = 19.
For more info take a look at this link

Abdelhak
  • 8,299
  • 4
  • 22
  • 36