Truncating UTF String to the given number of bytes while preserving its validity [for DB insert]

Often you need to insert a String from Java into a database column with a fixed length specified in bytes. Using string.substring(0, DB_FIELD_LENGTH); isn’t enough because it only cuts down the number of characters but in UTF-8 a single character may be represented by 1-4 bytes. But you cannot just turn the string into anContinue reading “Truncating UTF String to the given number of bytes while preserving its validity [for DB insert]”