Hi,
The DB2 column size in bytes. I want to cut off the strings by number of bytes to fit in.
I code the method using the range from https://en.wikipedia.org/wiki/UTF-8
But I saw some other different ranges and don't know what is the correct way to cut by the bytes.
Thank you very much
public static String truncateMaxBytes(String pString, int maxBytes) { int byteCount = 0; for (int i = 0; i < pString.length(); i++) { char c = pString.charAt(i); int bytesOfChar=0; if (c <= 0x007f) { bytesOfChar = 1; } else if (c <= 0x07FF) { bytesOfChar = 2; } else if (c <= 0xFFFF) { bytesOfChar = 3; } else if (c <= 0x1FFFFF) { bytesOfChar = 4; } else if (c <= 0x3FFFFFF) { bytesOfChar = 5; } else { bytesOfChar = 6; } byteCount += bytesOfChar; if (byteCount> maxBytes) { return pString.substring(0, i); } } return pString; }