Skip to content

Instantly share code, notes, and snippets.

@zephiransas
Created May 9, 2012 01:10
Show Gist options
  • Save zephiransas/2640912 to your computer and use it in GitHub Desktop.
Save zephiransas/2640912 to your computer and use it in GitHub Desktop.
substringByte
public static String substringByte(String str, int length) {
int len = 0;
StringBuffer buff = new StringBuffer();
if(str == null) return null;
for(int i = 0;i < str.length();i++) {
if(str.charAt(i) <= 0x7f) {
len += 1;
} else {
len += 2;
}
if(len <= length) {
buff.append(str.substring(i, i + 1));
} else {
break;
}
}
return buff.toString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment