I'm extracting UTF16 strings from a byte array, the strings are separated by one or more NULL(U+0000), when I print the converted string, it trims the NULL automatically, the below example program outputs "WinWin"
import java.nio.charset.Charset;
class Ucs {
public static void main(String[] args) {
byte[] b = new byte[] {87, 0, 105, 0, 110, 0, 0, 0, 0, 0, 87, 0, 105, 0, 110, 0, 0, 0};
String s = new String(b, Charset.forName("UTF-16LE"));
System.out.println(s);
}
}
but I want them separated like "Win Win", only if I split s
by something. I know I can find the NULL character in a for loop, which is really nasty. Is there a simple way to do this?
You can use split
:
System.out.println(Arrays.asList(s.split("\\00")));