using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
namespace PKE.Global
{
public class GeneralHelp
{
/// <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;
}
/// <summary>
/// 截取字符串不带“...”
/// </summary>
/// <param name="str">需要截取的字符串</param>
/// <param name="length">截取的长度</param>
/// <returns></returns>
public static string GetSubStringPrue(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]+"))
{
j += 4 / 3.0;
}
else
{
j += 1;
}
if (j <= length)
{
k += 1;
}
if (j >= length)
{
return temp.Substring(0, k);
}
}
return temp;
}
/// <summary>
/// 去除HTML标签
/// </summary>
/// <param name="htmlstring"></param>
/// <returns></returns>
public static string DelHtml(string htmlstring)
{
#region
//删除脚本
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", "");
#endregion
return htmlstring;
}
/// <summary>
/// 将特殊字符转换成转义符[适用于评论]
/// </summary>
/// <param name="content"></param>
/// <returns></returns>
public static string ReplaceSpecialMarkToCode(string content)
{
#region
return content.Replace("<", "<").Replace(">", ">");
#endregion
}
/// <summary>
/// 去除html和空格
/// </summary>
/// <param name="content"></param>
/// <returns></returns>
public static string DelHtmlAndSpace(string content)
{
return Regex.Replace(content, @"<(.[^>]*)>|(\s|( ))", "", RegexOptions.IgnoreCase); ;
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
namespace PKE.Global
{
public class GeneralHelp
{
/// <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;
}
/// <summary>
/// 截取字符串不带“...”
/// </summary>
/// <param name="str">需要截取的字符串</param>
/// <param name="length">截取的长度</param>
/// <returns></returns>
public static string GetSubStringPrue(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]+"))
{
j += 4 / 3.0;
}
else
{
j += 1;
}
if (j <= length)
{
k += 1;
}
if (j >= length)
{
return temp.Substring(0, k);
}
}
return temp;
}
/// <summary>
/// 去除HTML标签
/// </summary>
/// <param name="htmlstring"></param>
/// <returns></returns>
public static string DelHtml(string htmlstring)
{
#region
//删除脚本
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", "");
#endregion
return htmlstring;
}
/// <summary>
/// 将特殊字符转换成转义符[适用于评论]
/// </summary>
/// <param name="content"></param>
/// <returns></returns>
public static string ReplaceSpecialMarkToCode(string content)
{
#region
return content.Replace("<", "<").Replace(">", ">");
#endregion
}
/// <summary>
/// 去除html和空格
/// </summary>
/// <param name="content"></param>
/// <returns></returns>
public static string DelHtmlAndSpace(string content)
{
return Regex.Replace(content, @"<(.[^>]*)>|(\s|( ))", "", RegexOptions.IgnoreCase); ;
}
}
}