I create the following for truncating a string in java to a new string with a given number of bytes.
String truncatedValue = \"\";
String curren
I've improved upon Peter Lawrey's solution to accurately handle surrogate pairs. In addition, I optimized based on the fact that the maximum number of bytes per char
in UTF-8 encoding is 3.
public static String substring(String text, int maxBytes) {
for (int i = 0, len = text.length(); (len - i) * 3 > maxBytes;) {
int j = text.offsetByCodePoints(i, 1);
if ((maxBytes -= text.substring(i, j).getBytes(StandardCharsets.UTF_8).length) < 0)
return text.substring(0, i);
i = j;
}
return text;
}