1

I am trying to split my string using '|' operator but unable to complete this operation.

I am getting wrong output.

I am using below code :

public static void main(String[] args) {
        String str = "C|9374052566|TFNAME|TLNAME|01-10-1988|test@gmail.com|234897238794";
        String array[] = str.split("|");

        for (String str_ : array) {
            System.out.println(str_);
        }


    }
}

//Produce output
        C
        |
        9
        3
        7
        4
        0
        5
        2
        5
        6
        6
        |
        T
        F
        N
        A
        M
        E
        |
        T
        L
        N
        A
        M
        E
        |
        0
        1
        -
        1
        0
        -
        1
        9
        8
        8
        |
        t
        e
        s
        t
        @
        g
        m
        a
        i
        l
        .
        c
        o
        m
        |
        2
        3
        4
        8
        9
        7
        2
        3
        8
        7
        9
        4

        //Expected output
        C
        9374052566
        TFNAME
        TLNAME
        01-10-1988
        test@gmail.com
        234897238794

What is wrong with the program?

Shiladittya Chakraborty
  • 4,270
  • 8
  • 45
  • 94

1 Answers1

1

Split method takes regular expression and | is reserved character, which is used as an OR operator in the regular expression.

Use \\| to escape it.

akash
  • 22,664
  • 11
  • 59
  • 87