Search code examples
javastringutf-8splitiso-8859-1

Java Split ISO-8859-1 String with "broken vertical bar"


I read from a third system an ISO-8859-1 coded String. I have to split this String with the character ¦. This char has in ISO-8859-1 the value 166. The following code doesn't work, because the value in Java (UTF-8) of ¦ is 65533.

String [] parts = isoString.split("¦");

I am stuck... How can I solve this? Thanks


Solution

  • Working code:

    String s = new String(new byte[] {'a', 'b', (byte) 166, 'c', 'd'}, 
                          StandardCharsets.ISO_8859_1);
    String[] split = s.split("\u00a6");
    System.out.println("split = " + Arrays.toString(split));
    // prints split = [ab, cd]