287 lines
10 KiB
C#
287 lines
10 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using System.Text.RegularExpressions;
|
|
using System.Collections;
|
|
|
|
namespace Zhaizj.Framework.Utils
|
|
{
|
|
/// <summary>
|
|
/// 字符串操作帮助类
|
|
/// </summary>
|
|
public static partial class StringHelper
|
|
{
|
|
/// <summary>
|
|
/// 分割字符串
|
|
/// </summary>
|
|
public static string[] SplitString(string strContent, string strSplit)
|
|
{
|
|
if (!String.IsNullOrEmpty(strContent))
|
|
{
|
|
if (strContent.IndexOf(strSplit) < 0)
|
|
{
|
|
string[] tmp = { strContent };
|
|
return tmp;
|
|
}
|
|
return Regex.Split(strContent, Regex.Escape(strSplit), RegexOptions.IgnoreCase);
|
|
}
|
|
else
|
|
{
|
|
return new string[0] { };
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 分割字符串
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public static string[] SplitString(string strContent, string strSplit, int count)
|
|
{
|
|
string[] result = new string[count];
|
|
|
|
string[] splited = SplitString(strContent, strSplit);
|
|
|
|
for (int i = 0; i < count; i++)
|
|
{
|
|
if (i < splited.Length)
|
|
result[i] = splited[i];
|
|
else
|
|
result[i] = string.Empty;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 过滤字符串数组中每个元素为合适的大小
|
|
/// 当长度小于minLength时,忽略掉,-1为不限制最小长度
|
|
/// 当长度大于maxLength时,取其前maxLength位
|
|
/// 如果数组中有null元素,会被忽略掉
|
|
/// </summary>
|
|
/// <param name="minLength">单个元素最小长度</param>
|
|
/// <param name="maxLength">单个元素最大长度</param>
|
|
/// <returns></returns>
|
|
public static string[] PadStringArray(string[] strArray, int minLength, int maxLength)
|
|
{
|
|
if (minLength > maxLength)
|
|
{
|
|
int t = maxLength;
|
|
maxLength = minLength;
|
|
minLength = t;
|
|
}
|
|
|
|
int iMiniStringCount = 0;
|
|
for (int i = 0; i < strArray.Length; i++)
|
|
{
|
|
if (minLength > -1 && strArray[i].Length < minLength)
|
|
{
|
|
strArray[i] = null;
|
|
continue;
|
|
}
|
|
if (strArray[i].Length > maxLength)
|
|
{
|
|
strArray[i] = strArray[i].Substring(0, maxLength);
|
|
}
|
|
iMiniStringCount++;
|
|
}
|
|
|
|
string[] result = new string[iMiniStringCount];
|
|
for (int i = 0, j = 0; i < strArray.Length && j < result.Length; i++)
|
|
{
|
|
if (strArray[i] != null && strArray[i] != string.Empty)
|
|
{
|
|
result[j] = strArray[i];
|
|
j++;
|
|
}
|
|
}
|
|
|
|
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 分割字符串
|
|
/// </summary>
|
|
/// <param name="strContent">被分割的字符串</param>
|
|
/// <param name="strSplit">分割符</param>
|
|
/// <param name="ignoreRepeatItem">忽略重复项</param>
|
|
/// <param name="maxElementLength">单个元素最大长度</param>
|
|
/// <returns></returns>
|
|
public static string[] SplitString(string strContent, string strSplit, bool ignoreRepeatItem, int maxElementLength)
|
|
{
|
|
string[] result = SplitString(strContent, strSplit);
|
|
|
|
return ignoreRepeatItem ? DistinctStringArray(result, maxElementLength) : result;
|
|
}
|
|
|
|
public static string[] SplitString(string strContent, string strSplit, bool ignoreRepeatItem, int minElementLength, int maxElementLength)
|
|
{
|
|
string[] result = SplitString(strContent, strSplit);
|
|
|
|
if (ignoreRepeatItem)
|
|
{
|
|
result = DistinctStringArray(result);
|
|
}
|
|
return PadStringArray(result, minElementLength, maxElementLength);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 分割字符串
|
|
/// </summary>
|
|
/// <param name="strContent">被分割的字符串</param>
|
|
/// <param name="strSplit">分割符</param>
|
|
/// <param name="ignoreRepeatItem">忽略重复项</param>
|
|
/// <returns></returns>
|
|
public static string[] SplitString(string strContent, string strSplit, bool ignoreRepeatItem)
|
|
{
|
|
return SplitString(strContent, strSplit, ignoreRepeatItem, 0);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 清除字符串数组中的重复项
|
|
/// </summary>
|
|
/// <param name="strArray">字符串数组</param>
|
|
/// <param name="maxElementLength">字符串数组中单个元素的最大长度</param>
|
|
/// <returns></returns>
|
|
public static string[] DistinctStringArray(string[] strArray, int maxElementLength)
|
|
{
|
|
Hashtable h = new Hashtable();
|
|
|
|
foreach (string s in strArray)
|
|
{
|
|
string k = s;
|
|
if (maxElementLength > 0 && k.Length > maxElementLength)
|
|
{
|
|
k = k.Substring(0, maxElementLength);
|
|
}
|
|
h[k.Trim()] = s;
|
|
}
|
|
|
|
string[] result = new string[h.Count];
|
|
|
|
h.Keys.CopyTo(result, 0);
|
|
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 清除字符串数组中的重复项
|
|
/// </summary>
|
|
/// <param name="strArray">字符串数组</param>
|
|
/// <returns></returns>
|
|
public static string[] DistinctStringArray(string[] strArray)
|
|
{
|
|
return DistinctStringArray(strArray, 0);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取转换后的脚本文本字符
|
|
/// </summary>
|
|
/// <param name="msg">要转换的文本</param>
|
|
/// <returns></returns>
|
|
public static string GetValidScriptMsg(string msg)
|
|
{
|
|
return msg.Replace("\"", "\\\"").Replace("'","\'");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 将指定的字符转换成 base64 编码
|
|
/// </summary>
|
|
/// <param name="content">要进行编码的内容</param>
|
|
/// <returns></returns>
|
|
public static string ToBase64String(string content)
|
|
{
|
|
byte[] bytes = Encoding.Default.GetBytes(content);
|
|
return Convert.ToBase64String(bytes);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 将进行base64编码后的字符转换成正常字符
|
|
/// </summary>
|
|
/// <param name="content">要进行解码的内容</param>
|
|
/// <returns></returns>
|
|
public static string FromBase64String(string content)
|
|
{
|
|
byte[] outputb = Convert.FromBase64String(content);
|
|
return Encoding.Default.GetString(outputb);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取文本内容指定长度的摘要
|
|
/// </summary>
|
|
/// <param name="html">要处理的包含HTML代码的文本内容</param>
|
|
/// <param name="summaryLength">返回的长度</param>
|
|
/// <param name="thumChar">后缀符号</param>
|
|
/// <returns></returns>
|
|
public static string GetSummary(string html, int summaryLength,string thumChar)
|
|
{
|
|
string text = GetAllInnerText(html);
|
|
if (summaryLength >= text.Length)
|
|
return text;
|
|
return string.Format("{0}{1}",text.Substring(0, summaryLength),StringHelper.Repeat(thumChar,3));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 去除HTML标记返回纯文本
|
|
/// </summary>
|
|
/// <param name="html"></param>
|
|
/// <returns></returns>
|
|
public static string GetAllInnerText(string html)
|
|
{
|
|
return System.Text.RegularExpressions.Regex.Replace(html, @"<[^>]*>", "");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 去除HTML标记,返回纯文本
|
|
/// </summary>
|
|
/// <param name="NoHTML">包括HTML的源码 </param>
|
|
/// <returns>已经去除后的文字</returns>
|
|
public static string GetInnerText(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", "");
|
|
return Htmlstring;
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// 返回指定的字符重复N次后的结果
|
|
/// </summary>
|
|
/// <param name="str">重复的字符</param>
|
|
/// <param name="n">重复的次数</param>
|
|
/// <returns></returns>
|
|
public static string Repeat(string str, int n)
|
|
{
|
|
if (String.IsNullOrEmpty(str) || n <= 0)
|
|
return str;
|
|
StringBuilder sb = new StringBuilder();
|
|
while (n > 0)
|
|
{
|
|
sb.Append(str);
|
|
n--;
|
|
}
|
|
return sb.ToString();
|
|
}
|
|
}
|
|
}
|