using System; using System.Collections.Generic; using System.Text; using System.Text.RegularExpressions; using System.Collections; namespace Zhaizj.Framework.Utils { /// /// 字符串操作帮助类 /// public static partial class StringHelper { /// /// 分割字符串 /// 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] { }; } } /// /// 分割字符串 /// /// 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; } /// /// 过滤字符串数组中每个元素为合适的大小 /// 当长度小于minLength时,忽略掉,-1为不限制最小长度 /// 当长度大于maxLength时,取其前maxLength位 /// 如果数组中有null元素,会被忽略掉 /// /// 单个元素最小长度 /// 单个元素最大长度 /// 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; } /// /// 分割字符串 /// /// 被分割的字符串 /// 分割符 /// 忽略重复项 /// 单个元素最大长度 /// 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); } /// /// 分割字符串 /// /// 被分割的字符串 /// 分割符 /// 忽略重复项 /// public static string[] SplitString(string strContent, string strSplit, bool ignoreRepeatItem) { return SplitString(strContent, strSplit, ignoreRepeatItem, 0); } /// /// 清除字符串数组中的重复项 /// /// 字符串数组 /// 字符串数组中单个元素的最大长度 /// 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; } /// /// 清除字符串数组中的重复项 /// /// 字符串数组 /// public static string[] DistinctStringArray(string[] strArray) { return DistinctStringArray(strArray, 0); } /// /// 获取转换后的脚本文本字符 /// /// 要转换的文本 /// public static string GetValidScriptMsg(string msg) { return msg.Replace("\"", "\\\"").Replace("'","\'"); } /// /// 将指定的字符转换成 base64 编码 /// /// 要进行编码的内容 /// public static string ToBase64String(string content) { byte[] bytes = Encoding.Default.GetBytes(content); return Convert.ToBase64String(bytes); } /// /// 将进行base64编码后的字符转换成正常字符 /// /// 要进行解码的内容 /// public static string FromBase64String(string content) { byte[] outputb = Convert.FromBase64String(content); return Encoding.Default.GetString(outputb); } /// /// 获取文本内容指定长度的摘要 /// /// 要处理的包含HTML代码的文本内容 /// 返回的长度 /// 后缀符号 /// 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)); } /// /// 去除HTML标记返回纯文本 /// /// /// public static string GetAllInnerText(string html) { return System.Text.RegularExpressions.Regex.Replace(html, @"<[^>]*>", ""); } /// /// 去除HTML标记,返回纯文本 /// /// 包括HTML的源码 /// 已经去除后的文字 public static string GetInnerText(string Htmlstring) { //删除脚本 Htmlstring = Regex.Replace(Htmlstring, @"]*?>.*?", "", 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, @"