/** * 将字符串截短,取前n个字符,英文算半个字符。 * @param orignalString 原字符串 * @param length 长度 * @param chopedString 超过部分的表示字符串 * @return 截取的字符串 */
public static String chop(
String orignalString,
int length,
String chopedString) {
if (orignalString==
null||orignalString.
length()==0) {
return orignalString; } orignalString=orignalString.replaceAll(" "," ");
if (orignalString.
length()<
length) {
return orignalString; }
StringBuffer buffer=
new StringBuffer(
length);
length=
length*2;
int count=0;
int stringLength=orignalString.
length();
int i=0;
for (;count<
length&&i<stringLength;i++) {
char c=orignalString.charAt(i);
if (c<'\u00ff') { count++; }
else { count+=2; } buffer.append(c); }
if (i<stringLength) { buffer.append(chopedString); }
return buffer.toString(); }
本文地址:http://com.8s8s.com/it/it9615.htm