Skip to content

Instantly share code, notes, and snippets.

View nidev's full-sized avatar

Changbeom Yun (Nidev) nidev

View GitHub Profile
@nidev
nidev / utf8truncate.java
Created April 5, 2017 07:36 — forked from lpar/utf8truncate.java
Truncate a Java string so that its UTF-8 representation will not exceed the specified number of bytes
/**
* Truncate a Java string so that its UTF-8 representation will not
* exceed the specified number of bytes.
*
* For discussion of why you might want to do this, see
* http://lpar.ath0.com/2011/06/07/unicode-alchemy-with-db2/
*/
public static String utf8truncate(String input, int length) {
StringBuffer result = new StringBuffer(length);
int resultlen = 0;