#region 文本中字符的转换
/// <summary>
/// 将文本格式转换为html代码
/// </summary>
/// <param name="str">要格式化的字符串</param>
/// <returns>格式化后的字符串</returns>
public static String ToHtml(string str)
{
if (str == null || str.Equals(""))
{
return str;
}
StringBuilder sb = new StringBuilder(str);
sb.Replace("&", "&");
sb.Replace("<", "<");
sb.Replace(">", ">");
sb.Replace("\r\n", "<br>");
sb.Replace("\n", "<br>");
sb.Replace("\t", " ");
sb.Replace(" ", " ");
return sb.ToString();
}
/// <summary>
/// 将HTML代码转化成文本格式
/// </summary>
/// <param name="str">要格式化的字符串</param>
/// <returns>格式化后的字符串</returns>
public static String ToTxt(String str)
{
if (str == null || str.Equals(""))
{
return str;
}
StringBuilder sb = new StringBuilder(str);
sb.Replace(" ", " ");
sb.Replace("<br>", "\r\n");
sb.Replace("<", "<");
sb.Replace(">", ">");
sb.Replace("&", "&");
return sb.ToString();
}
/// <summary>
/// 截取字符串的函数,返回一个string
/// </summary>
/// <param name="RawString">要截取的字符串</param>
/// <param name="Length">截取的字数</param>
/// <returns></returns>
public static string GetCutString(string RawString, Int32 Length)
{
if (RawString.Length <= Length)
{
return RawString;
}
else
{
return RawString.Substring(0, Length) + "...";
}
}
#endregion
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。