/**
* @param len
* 需要显示的长度(<font color="red">注意:长度是以byte为单位的,一个汉字是2个byte</font>)
* @param symbol
* 用于表示省略的信息的字符,如“...”,“>>>”等。
* @return 返回处理后的字符串
*/
public static String getLimitLengthString(String str) {
String symbol="...";
int len = 25;
// int counterOfDoubleByte;
byte b[];
String result="";
// counterOfDoubleByte = 0;
try {
b = str.getBytes("GBK");
if (b.length <= len)
return str;
// for (int i = 0; i < len; i++) {
// if (b[i] < 0)
// counterOfDoubleByte++;
// }
//
// if (counterOfDoubleByte % 2 == 0) {
// result=new String(b, 0, 10, "GBK")+symbol+new String(b, b.length-12, 12, "GBK");
// } else {
// result=new String(b, 0, 10, "GBK")+symbol+new String(b, b.length-12, 12, "GBK");
// }
result=new String(b, 0, 10, "GBK")+symbol+new String(b, b.length-12, 12, "GBK");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return result;
}
* @param len
* 需要显示的长度(<font color="red">注意:长度是以byte为单位的,一个汉字是2个byte</font>)
* @param symbol
* 用于表示省略的信息的字符,如“...”,“>>>”等。
* @return 返回处理后的字符串
*/
public static String getLimitLengthString(String str) {
String symbol="...";
int len = 25;
// int counterOfDoubleByte;
byte b[];
String result="";
// counterOfDoubleByte = 0;
try {
b = str.getBytes("GBK");
if (b.length <= len)
return str;
// for (int i = 0; i < len; i++) {
// if (b[i] < 0)
// counterOfDoubleByte++;
// }
//
// if (counterOfDoubleByte % 2 == 0) {
// result=new String(b, 0, 10, "GBK")+symbol+new String(b, b.length-12, 12, "GBK");
// } else {
// result=new String(b, 0, 10, "GBK")+symbol+new String(b, b.length-12, 12, "GBK");
// }
result=new String(b, 0, 10, "GBK")+symbol+new String(b, b.length-12, 12, "GBK");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return result;
}