Strings In Depth17 / 18
Up next

String and bytes

import java.nio.charset.StandardCharsets;

class Main {
    public static void main(String[] args) {
        String s = "café";
        byte[] data = s.getBytes(StandardCharsets.UTF_8);
        // 4 chars, but 5 bytes in UTF-8
        System.out.println(s.length() + " " + data.length);
        System.out.println(new String(data, StandardCharsets.UTF_8));
    }
}
What you're solving

encode to UTF-8 and back