#region 格式化字串,取字串前 strLength 位,其他的用...代替.計算字串長度。漢字兩個位元組,字母一個位元組FormatString(string str,int len)
/// <summary>
/// 格式化字串,取字串前 strLength 位,其他的用...代替.計算字串長度。漢字兩個位元組,字母一個位元組
/// </summary>
/// <param name="str">字串</param>
/// <param name="strLength">字串長度</param>
/// <returns></returns>
public static string FormatStr(string str,int len)
{
ASCIIEncoding ascii = new ASCIIEncoding();
int tempLen=0;
string tempString="";
byte[] s = ascii.GetBytes(str);
for(int i=0;i<s.Length;i++)
{
if((int)s[i]==63)
{
tempLen+=2;
}
else
{
tempLen+=1;
}
try
{
tempString+=str.Substring(i,1);
}
catch
{
break;
}
if(tempLen>len)
break;
}
//如果截過則加上半個省略符號
byte[] mybyte=System.Text.Encoding.Default.GetBytes(str);
if(mybyte.Length>len)
tempString+="...";
tempString=tempString.Replace(" "," ");
tempString=tempString.Replace("<","<");
tempString=tempString.Replace(">",">");
tempString=tempString.Replace('\n'.ToString(),"<br>");
return tempString;
}
#endregion