1

Already gone through How to split a string in Java

I have string str = "He|is|a|very|very|good|boy||isn|t|he|"; and want to split on the basis "|". But split function not working...

{

 String[] sa = sr.split("|");

 for(String s1 : sa){
            System.out.println(s1);
    }
}

Getting output ..

H
e
|
i
s
|
a
|
v
e
r
y
|
v
e
r
y
|
g
o
o
d
|
b
o
y
|
|
i
s
n
|
t
|
h
e
|
Community
  • 1
  • 1
surendrapanday
  • 770
  • 2
  • 9
  • 24

1 Answers1

0

This

sr.split("|");

won't work unless you escape | character using \ but \ is also a character so you also need to escape it using another \. So instead of ("|"); you need ("\\|");

Change

String[] sa = sr.split("|");

to

String[] sa = sr.split("\\|");
Yousaf
  • 27,861
  • 6
  • 44
  • 69