0
String line = "First string March 8, # 2017: Boris#|#Second string";
String[] list = line.split("#|#");

i was expecting list[0] = "First string March 8, # 2017: Boris" and

list[1] = "Second string"

But i am not getting the result as expected . its get split to multiple strings. whats the change i need to do in split function ?

Koottalida
  • 25
  • 6

3 Answers3

3
String[] list = line.split("#\\|#");

The split() method's (first) parameter is expected to contain a regular expression. The | is a special character is Regex, so you need to escape it with \ to represent it in a regex literally.

Dmitry Egorov
  • 9,542
  • 3
  • 22
  • 40
0

You need to escape the pipe: #\\|#

example:

 String line = "First string March 8, # 2017: Boris#|#Second string";
 String[] list = line.split("#\\|#");
 System.out.println(Arrays.toString(list));
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0

The split() method doesn't expect normal strings, but regular expressions. Thus you need to escape the | char; so go for:

 split("#\\|#");
GhostCat
  • 137,827
  • 25
  • 176
  • 248