public static string CutString(string inputString,int len)
{
ASCIIEncoding ascii = new ASCIIEncoding();
int tempLen=0;
string tempString="";
byte[] s = ascii.GetBytes(inputString);
for(int i=0;i<s.Length;i++)
{
if((int)s[i]==63)
{
tempLen+=2;
}
else
{
tempLen+=1;
}
try
{
tempString+=inputString.Substring(i,1);
}
catch
{
break;
}
if(tempLen>len)
break;
}
//如果截过则加上半个省略号
byte[] mybyte=System.Text.Encoding.Default.GetBytes(inputString);
if(mybyte.Length>len)
tempString+="…";
return tempString;
}
#endregion
注:ASCIIEncoding 在system.text;
本文介绍了一个用于字符串截取的公共函数,该函数能够根据指定长度截取字符串,并考虑了ASCII编码下不同字符的长度差异。同时,在截断后会适当添加省略号以指示原始字符串已被截短。

605

被折叠的 条评论
为什么被折叠?



