日期:2011-06-12 浏览次数:20494 次
///<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;
}