0

Im running this code and getting the wrong output. It seems like | as the expression in split is giving wrong output

public static void main(String[] args) {
    String s = "Hi, |+nl+|+nl+| split me please";
    String[] sArr = s.split("|");

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

Output:

H
i
,

|
+
n
l
+
|
+
n
l
+
|

s
p
l
i
t

m
e

p
l
e
a
s
e

How can I split it properly that it gets split this way? Hi, +n1+ +n1+ and so forth

Nate
  • 79
  • 1
  • 4
  • 12

1 Answers1

0

You may have to escape the |.

String[] sArr = s.split("\\|");