You need the encoding in the database to match the encoding for java strings. Alternatively, you can convert the string using something like this and get a length that matches the encoding in the database. This will give you the exact number of bytes. Otherwise, you still hope that the encodings will match.
String string = "Rückruf ins Ausland"; int curByteCount = 0; String nextChar; for(int index = 0; curByteCount + (nextChar = string.substr(index,index + 1)).getBytes("UTF-8").length < trimmedBytes.length; index++){ curByteCount += nextChar.getBytes("UTF-8").length; } byte[] subStringBytes = new byte[10]; System.arraycopy(string.getBytes("UTF-8"), 0, subStringBytes, 0, curByteCount); String trimed = new String(subStringBytes, "UTF-8");
That should do it. In addition, shootln't truncate the multibyte character in the process. The database is assumed to be UTF-8 encoded. Another assumption is that the string should actually be truncated.
source share