Java is not splitting a String correctly

Asked

Viewed 49 times

1

I am trying to split a string using the following character "|" But java is understood to divide letter by letter.

        List<Curso> cursos = new ArrayList<Curso>();
        List<String> pacotes = Arrays.asList("Criança-1,2,3-1,00|Preparatório Primeiro Emprego-4,5,6,7,8,9-139,99".split("|"));
    System.out.println(pacotes.toString()); //I/System.out: [, C, r, i, a, n, ç, a, -, 1, ,, 2, ,, 3, -, 1, ,, 0, 0, |, P, r, e, p, a, r, a, t, ó, r, i, o,  , P, r, i, m, e, i, r, o,  , E, m, p, r, e, g, o, -, 4, ,, 5, ,, 6, ,, 7, ,, 8, ,, 9, -, 1, 3, 9, ,, 9, 9]

Can someone help me?

1 answer

8


The pipe "|" is a special character of regex. Therefore, you should escape it:

split("\\|");
  • Thanks Amigo!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.