Format string in Java, in bytes
Suppose I have a string containing non ASCII characters in Java, I want to use string Format () so that the formatted string will have the minimum width of the string byte length
String s = "æøå"; String.format(l,"%" + 10 + "s",s);
This will result in a string with 7 leading spaces
But what I want is that there should be only 4 leading spaces, because the size of the original string is 6 bytes
This seems to be a common requirement, so I want to ask if there are any built classes that can implement this, or should I implement the formattable interface myself?
Solution
A string does not have multiple bytes - it has many characters The number of bytes required to represent a string depends on the encoding you use I don't know how to do any built-in things in filling (I don't think this is a common requirement) You can ask charsetencoder for the maximum and average number of bytes per character, but I don't see any way to get the number of bytes of a specific string without basically encoding:
Charset cs = Charset.forName("UTF-8"); ByteBuffer buffer = cs.encode("foobar"); int lengthInBytes = buffer.remaining();
If you want to encode a string, you may just want to perform the encoding, calculate how much padding is required, then write the encoded padding, and then write the encoded text It really depends on how you handle the data