strLen(string str,int len) 取字符串的前len个字符,纠正中英文都算一个单位的问题
Posted on 2006-03-14 10:14 King0502 阅读(162) 评论(0) 编辑 收藏 举报
/// <summary>
/// 取字符串的前len个字符,len以中文字符宽度为单位
/// </summary>
/// <param name="str"></param>
/// <param name="len"></param>
/// <returns></returns>
public static string strLen(string str,int len){
string strNew = "";
int strLen=0;
for(int i=0;i<str.Length;i++){
if((int)str[i]>255)
{
strLen += 2;
strNew += str[i].ToString();
}
else
{
strLen += 1;
strNew += str[i].ToString();
}
if(strLen >= len * 2)
return strNew + "...";
}
return strNew;
}
/// 取字符串的前len个字符,len以中文字符宽度为单位
/// </summary>
/// <param name="str"></param>
/// <param name="len"></param>
/// <returns></returns>
public static string strLen(string str,int len){
string strNew = "";
int strLen=0;
for(int i=0;i<str.Length;i++){
if((int)str[i]>255)
{
strLen += 2;
strNew += str[i].ToString();
}
else
{
strLen += 1;
strNew += str[i].ToString();
}
if(strLen >= len * 2)
return strNew + "...";
}
return strNew;
}