1。
/// <summary>
/// 去除html和空格( )
/// </summary>
/// <param name="content"></param>
/// <returns></returns>
public static string DelHtmlAndSpace(string content)
{
return Regex.Replace(content, @"<(.[^>]*)>|(/s|( ))", "", RegexOptions.IgnoreCase); ;
}
2。
/// <summary>
/// 将特殊字符转换成转义符[适用于评论]
/// </summary>
/// <param name="content"></param>
/// <returns></returns>
public static string ReplaceSpecialMarkToCode(string content)
{
#region
return content.Replace("<", "<").Replace(">", ">");
#endregion
}
3。
此方法还有许多改进的地方
/// <summary>
/// 截取字符串
/// </summary>
/// <param name="str">需要截取的字符串</param>
/// <param name="length">截取的长度</param>
/// <returns></returns>
public static string GetSubString(string str, int length)
{
string temp =str;
double j = 0;
int k = 0;
for (int i = 0; i < temp.Length; i++)
{
if (Regex.IsMatch(temp.Substring(i, 1), @"[/u4e00-/u9fa5|/u3001-/u3002|/uff1f|/uff01|/uff0c|/uff1a-/uff1b|/u2018-/u2019|/u201c-/u201d|/u3014-/u3015|/u3010-/u3011|/u2014|/u3008-/u3009]+"))
//汉字和中文标点符号(、。、?、!、,、:;、‘’、“”、()、【】、—、《》)
{
j += 2;
}
else if (Regex.IsMatch(temp.Substring(i, 1), @"[/u0041-/u005a]+"))//大写字母ABC..
{
j += 4 / 3.0;
}
else
{
j += 1;
}
if (j <= length)
{
k += 1;
}
if (j >= length)
{
return temp.Substring(0, k) + "...";
}
}
return temp;
}