0

I am trying to split a string to List where the delimiter is the $_$.

For example the text Lorem ipsum dolor $$ sit amet, consectetur adipiscing $$ elit. Aliquam $_$ eu. I would like to convert to o list with elements

el1= Lorem ipsum dolor
el2= sit amet, consectetur adipiscing
el3- elit. Aliquam 
el4= eu.

I tried the code bellow with no success.Is there any other way?

List<String> myList = new ArrayList<String>(Arrays.asList(s.split("$_$")));
charkoul
  • 67
  • 1
  • 9

1 Answers1

3
List<String> myList = new ArrayList<>(Arrays.asList(s.split("\\$_\\$")));
List<String> myList = Arrays.asList(s.split("\\$_\\$")); // or simply this

As $ has a special meaning (end-of-text) you need to regex-escape it by a backslash. In a String literal a backslash has to be escaped itself - with a backslash.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138