코드 사본은 다음과 같습니다.
/*
* Copyright 2012-2013 Haohui Network Corporation
*/
패키지 com.haohui.common.utils;
/**
* <fre>
* 스트링 도우미
* </pre>
*
* @project baidamei
* @author cevencheng <[email protected]>
* @Create 2012-11-30 2:42:56 PM
*/
공개 클래스 StringTool {
/**
*<b> 지정된 바이트 길이 문자열을 가로 채면 한자의 반을 반환 할 수 없습니다 </b>
*예를 들어:
*웹 페이지에 최대 17 자까지 표시 될 수 있다면 길이는 34입니다.
* StringTool.getSubstring (str, 34);
*
* @param str
* @param 길이
* @반품
*/
public static string getSubstring (String str, int length) {
int count = 0;
int 오프셋 = 0;
char [] c = str.tochararray ();
for (int i = 0; i <c.length; i ++) {
if (c [i]> 256) {
오프셋 = 2;
count += 2;
} 또 다른 {
오프셋 = 1;
카운트 ++;
}
if (count == length) {
return str.substring (0, i + 1);
}
if ((count == length + 1 && offset == 2)) {
return str.substring (0, i);
}
}
반품 "";
}
}