[Asp.Net]按字符串实际长度截取定长字符窜

///
/// 按字符串实际长度截取定长字符窜
///
/// 原字符串
/// 要截取的长度
/// string型字符串
public static string GetString(string str, int length)
{
int i = 0, j = 0;
foreach(char chr in str)
{
if((int)chr > 127)
{
i += 2;
}
else
{
i ++;
}
if (i > length)
{
str = str.Substring(0, j) + "...";
break;
}
j ++;
}
return str;

}


posted on 2007-02-01 19:59  自由.Net  阅读(602)  评论(0编辑  收藏  举报