Created
May 9, 2012 01:10
-
-
Save zephiransas/2640912 to your computer and use it in GitHub Desktop.
substringByte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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