///<summary>
///功能:精確的按要求截取指定長度的字符串
///中文算2個英文算一個
///參數:str 待截取字符,len 截取長度 symbol 表示字符(....)
///</summary>
public static string GotTopic(string str,int len,string symbol)
{
int count = 0;
string strTemp = "";
str = NoHtml(str);
for (int i = 0; i < str.Length; i++)
{
if (Math.Abs(((int)(str.Substring(i, 1).ToCharArray())[0])) > 255)
{
count += 2;
}else
{
count += 1;
}
if (count <= len)
{
strTemp += str.Substring(i, 1);
}
else
{
return strTemp + symbol;
}
}
return str;
}
/// <summary>
/// 過濾掉Html標簽
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string NoHtml(string str)
{
str = Regex.Replace(str, @"(\<.[^\<]*\>)", " ", RegexOptions.IgnorePatternWhitespace | RegexOptions.IgnoreCase);
str = Regex.Replace(str, @"(\<\/[^\<]*\>)"," ", RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
return str;
}