using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
namespace Common
{
public class StringHelper
{
public static string SubStr(string sString, int nLeng)
{
if (sString.Length <= nLeng)
{
return sString;
}
else
{
return sString.Substring(0, nLeng) + "...";
}
}
/// <summary>
/// 清除脚本
/// </summary>
/// <param name="Htmlstring"></param>
/// <returns></returns>
public static string NoHTML(string Htmlstring)
{
//删除脚本
Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
//删除HTML
Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"([/r/n])[/s]+", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "/"", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "/xa1", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "/xa2", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "/xa3", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "/xa9", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"&#(/d+);", "", RegexOptions.IgnoreCase);
Htmlstring.Replace("<", "");
Htmlstring.Replace(">", "");
Htmlstring.Replace("/r/n", "");
Htmlstring = HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim();
return Htmlstring;
}
/// <summary>
/// 生成指定长度的随机数字串
///</summary>
///<param name="length">长度</param>
///<returns></returns>
public static string RndStr(int length)
{
Random rnd = new Random();
string returnstring = "";
int i = 1;
while (i <= length)
{
returnstring += rnd.Next(0, 9).ToString();
i++;
}
return returnstring;
}
/// <summary>
/// 截取字符串,保留其HTML格式
/// </summary>
/// <param name="strText"></param>
/// <param name="len"></param>
/// <returns></returns>
public static string CutWithSubstring(string strText, int len)
{
if (strText.Length > len)
{
return strText.Substring(0, len) + "…";
}
else
{
return strText;
}
}
public static string LoseHtml(string ContentStr)
{
string ClsTempLoseStr = "";
System.Text.RegularExpressions.Regex regEx = new System.Text.RegularExpressions.Regex("<///*[^<>]*>");
ClsTempLoseStr = regEx.Replace(ContentStr, "");
return ClsTempLoseStr;
}
/// <summary>
/// 截取字符串去掉其HTML字符
/// </summary>
/// <param name="inputString"></param>
/// <param name="len"></param>
/// <returns></returns>
public static string CutWithOutHtml(string inputString, int len)
{
int tempLen = 0;
string tempString = "";
inputString = LoseHtml(inputString);
ASCIIEncoding ascii = new ASCIIEncoding();
byte[] s = ascii.GetBytes(inputString);
for (int i = 0; i < s.Length; i++)
{
if ((int)s[i] == 63)
{
tempLen += 2;
}
else
{
tempLen += 1;
}
try
{
tempString += inputString.Substring(i, 1);
}
catch
{
break;
}
if (tempLen > len)
break;
}
//如果截过则加上半个省略号
byte[] mybyte = System.Text.Encoding.Default.GetBytes(inputString);
if (mybyte.Length > len)
{
tempString = tempString.Substring(0, tempString.Length - 3 == 0 ? 1 : tempString.Length - 3) + "…";
}
return tempString;
}
}
}