コードコピーは次のとおりです。
/*
* Copyright 2012-2013 The Haohui Network Corporation
*/
パッケージcom.haohui.common.utils;
/**
* <pre>
*文字列ヘルパー
* </pre>
*
* @project baidamei
* @author cevencheng <[email protected]>
* @Create 2012-11-30 2:42:56 PM
*/
パブリッククラスStringTool {
/**
*<b>指定されたバイトの長さの文字列をインターセプトすることは、漢字の半分を返すことができません</b>
*例えば:
*Webページに最大17個の漢字を表示できる場合、長さは34です
* StringTool.getSubstring(str、34);
*
* @param str
* @paramの長さ
* @戻る
*/
public static string getsubstring(string str、int length){
int count = 0;
int offset = 0;
char [] c = str.tochararray();
for(int i = 0; i <c.length; i ++){
if(c [i]> 256){
オフセット= 2;
count += 2;
} それ以外 {
オフセット= 1;
count ++;
}
if(count == length){
str.substring(0、i + 1)を返します。
}
if((count == length + 1 && offset == 2)){
str.substring(0、i)を返します。
}
}
戻る "";
}
}