public static string GetStringByBytes(string str, int len)
{
if (str == null)
return string.Empty;

int j = 0, k = 0;
ASCIIEncoding encoding = new ASCIIEncoding();
for (int i = 0; i < str.Length; i++)
{
byte[] bytes = Encoding.Default.GetBytes(str.Substring(i, 1));
if (bytes.Length == 2)//不是英文
{
j = j + 2;
}
else
j++;

if (j <= len)
k += 1;

else if (j >= len)
return str.Substring(0, k - 2) + "...";
}
return str;
}

posted on 2015-09-21 13:53  星星点灯6  阅读(300)  评论(0编辑  收藏  举报