栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C# > C#教程

非常实用的C#字符串操作处理类StringHelper.cs

C#教程 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

非常实用的C#字符串操作处理类StringHelper.cs

一个非常好的C#字符串操作处理类StringHelper.cs,具体内容如下

///  
/// 类说明:Assistant 
/// 编 码 人:苏飞 
/// 联系方式:361983679  
/// 更新网站:http://www.sufeinet.com/thread-655-1-1.html 
///  
using System; 
using System.Collections.Generic; 
using System.Text; 
using System.Text.Regularexpressions; 
 
namespace DotNet.Utilities 
{ 
  ///  
  /// 字符串操作类 
  /// 1、GetStrArray(string str, char speater, bool toLower) 把字符串按照分隔符转换成 List 
  /// 2、GetStrArray(string str) 把字符串转 按照, 分割 换为数据 
  /// 3、GetArrayStr(List list, string speater) 把 List 按照分隔符组装成 string 
  /// 4、GetArrayStr(List list) 得到数组列表以逗号分隔的字符串 
  /// 5、GetArrayValueStr(Dictionary list)得到数组列表以逗号分隔的字符串 
  /// 6、DelLastComma(string str)删除最后结尾的一个逗号 
  /// 7、DelLastChar(string str, string strchar)删除最后结尾的指定字符后的字符 
  /// 8、ToSBC(string input)转全角的函数(SBC case) 
  /// 9、ToDBC(string input)转半角的函数(SBC case) 
  /// 10、GetSubStringList(string o_str, char sepeater)把字符串按照指定分隔符装成 List 去除重复 
  /// 11、GetCleanStyle(string StrList, string SplitString)将字符串样式转换为纯字符串 
  /// 12、GetNewStyle(string StrList, string NewStyle, string SplitString, out string Error)将字符串转换为新样式 
  /// 13、SplitMulti(string str, string splitstr)分割字符串 
  /// 14、SqlSafeString(string String, bool IsDel) 
  ///  
  public class StringHelper 
  { 
    ///  
    /// 把字符串按照分隔符转换成 List 
    ///  
    /// 源字符串 
    /// 分隔符 
    /// 是否转换为小写 
    ///  
    public static List GetStrArray(string str, char speater, bool toLower) 
    { 
      List list = new List(); 
      string[] ss = str.Split(speater); 
      foreach (string s in ss) 
      { 
 if (!string.IsNullOrEmpty(s) && s != speater.ToString()) 
 { 
   string strVal = s; 
   if (toLower) 
   { 
     strVal = s.ToLower(); 
   } 
   list.Add(strVal); 
 } 
      } 
      return list; 
    } 
    ///  
    /// 把字符串转 按照, 分割 换为数据 
    ///  
    ///  
    ///  
    public static string[] GetStrArray(string str) 
    { 
      return str.Split(new Char[] { ',' }); 
    } 
    ///  
    /// 把 List 按照分隔符组装成 string 
    ///  
    ///  
    ///  
    ///  
    public static string GetArrayStr(List list, string speater) 
    { 
      StringBuilder sb = new StringBuilder(); 
      for (int i = 0; i < list.Count; i++) 
      { 
 if (i == list.Count - 1) 
 { 
   sb.Append(list[i]); 
 } 
 else 
 { 
   sb.Append(list[i]); 
   sb.Append(speater); 
 } 
      } 
      return sb.ToString(); 
    } 
    ///  
    /// 得到数组列表以逗号分隔的字符串 
    ///  
    ///  
    ///  
    public static string GetArrayStr(List list) 
    { 
      StringBuilder sb = new StringBuilder(); 
      for (int i = 0; i < list.Count; i++) 
      { 
 if (i == list.Count - 1) 
 { 
   sb.Append(list[i].ToString()); 
 } 
 else 
 { 
   sb.Append(list[i]); 
   sb.Append(","); 
 } 
      } 
      return sb.ToString(); 
    } 
    ///  
    /// 得到数组列表以逗号分隔的字符串 
    ///  
    ///  
    ///  
    public static string GetArrayValueStr(Dictionary list) 
    { 
      StringBuilder sb = new StringBuilder(); 
      foreach (KeyValuePair kvp in list) 
      { 
 sb.Append(kvp.Value + ","); 
      } 
      if (list.Count > 0) 
      { 
 return DelLastComma(sb.ToString()); 
      } 
      else 
      { 
 return ""; 
      } 
    } 
 
    #region 删除最后一个字符之后的字符 
 
    ///  
    /// 删除最后结尾的一个逗号 
    ///  
    public static string DelLastComma(string str) 
    { 
      return str.Substring(0, str.LastIndexOf(",")); 
    } 
 
    ///  
    /// 删除最后结尾的指定字符后的字符 
    ///  
    public static string DelLastChar(string str, string strchar) 
    { 
      return str.Substring(0, str.LastIndexOf(strchar)); 
    } 
 
    #endregion 
 
    ///  
    /// 转全角的函数(SBC case) 
    ///  
    ///  
    ///  
    public static string ToSBC(string input) 
    { 
      //半角转全角: 
      char[] c = input.ToCharArray(); 
      for (int i = 0; i < c.Length; i++) 
      { 
 if (c[i] == 32) 
 { 
   c[i] = (char)12288; 
   continue; 
 } 
 if (c[i] < 127) 
   c[i] = (char)(c[i] + 65248); 
      } 
      return new string(c); 
    } 
 
    ///  
    /// 转半角的函数(SBC case) 
    ///  
    /// 输入 
    ///  
    public static string ToDBC(string input) 
    { 
      char[] c = input.ToCharArray(); 
      for (int i = 0; i < c.Length; i++) 
      { 
 if (c[i] == 12288) 
 { 
   c[i] = (char)32; 
   continue; 
 } 
 if (c[i] > 65280 && c[i] < 65375) 
   c[i] = (char)(c[i] - 65248); 
      } 
      return new string(c); 
    } 
 
    ///  
    /// 把字符串按照指定分隔符装成 List 去除重复 
    ///  
    ///  
    ///  
    ///  
    public static List GetSubStringList(string o_str, char sepeater) 
    { 
      List list = new List(); 
      string[] ss = o_str.Split(sepeater); 
      foreach (string s in ss) 
      { 
 if (!string.IsNullOrEmpty(s) && s != sepeater.ToString()) 
 { 
   list.Add(s); 
 } 
      } 
      return list; 
    } 
 
 
    #region 将字符串样式转换为纯字符串 
    ///  
    /// 将字符串样式转换为纯字符串 
    ///  
    ///  
    ///  
    ///  
    public static string GetCleanStyle(string StrList, string SplitString) 
    { 
      string RetrunValue = ""; 
      //如果为空,返回空值 
      if (StrList == null) 
      { 
 RetrunValue = ""; 
      } 
      else 
      { 
 //返回去掉分隔符 
 string NewString = ""; 
 NewString = StrList.Replace(SplitString, ""); 
 RetrunValue = NewString; 
      } 
      return RetrunValue; 
    } 
    #endregion 
 
    #region 将字符串转换为新样式 
    ///  
    /// 将字符串转换为新样式 
    ///  
    ///  
    ///  
    ///  
    ///  
    ///  
    public static string GetNewStyle(string StrList, string NewStyle, string SplitString, out string Error) 
    { 
      string ReturnValue = ""; 
      //如果输入空值,返回空,并给出错误提示 
      if (StrList == null) 
      { 
 ReturnValue = ""; 
 Error = "请输入需要划分格式的字符串"; 
      } 
      else 
      { 
 //检查传入的字符串长度和样式是否匹配,如果不匹配,则说明使用错误。给出错误信息并返回空值 
 int strListLength = StrList.Length; 
 int NewStyleLength = GetCleanStyle(NewStyle, SplitString).Length; 
 if (strListLength != NewStyleLength) 
 { 
   ReturnValue = ""; 
   Error = "样式格式的长度与输入的字符长度不符,请重新输入"; 
 } 
 else 
 { 
   //检查新样式中分隔符的位置 
   string Lengstr = ""; 
   for (int i = 0; i < NewStyle.Length; i++) 
   { 
     if (NewStyle.Substring(i, 1) == SplitString) 
     { 
Lengstr = Lengstr + "," + i; 
     } 
   } 
   if (Lengstr != "") 
   { 
     Lengstr = Lengstr.Substring(1); 
   } 
   //将分隔符放在新样式中的位置 
   string[] str = Lengstr.Split(','); 
   foreach (string bb in str) 
   { 
     StrList = StrList.Insert(int.Parse(bb), SplitString); 
   } 
   //给出最后的结果 
   ReturnValue = StrList; 
   //因为是正常的输出,没有错误 
   Error = ""; 
 } 
      } 
      return ReturnValue; 
    } 
    #endregion 
 
    ///  
    /// 分割字符串 
    ///  
    ///  
    ///  
    ///  
    public static string[] SplitMulti(string str, string splitstr) 
    { 
      string[] strArray = null; 
      if ((str != null) && (str != "")) 
      { 
 strArray = new Regex(splitstr).Split(str); 
      } 
      return strArray; 
    } 
    public static string SqlSafeString(string String, bool IsDel) 
    { 
      if (IsDel) 
      { 
 String = String.Replace("'", ""); 
 String = String.Replace(""", ""); 
 return String; 
      } 
      String = String.Replace("'", "'"); 
      String = String.Replace(""", """); 
      return String; 
    } 
 
    #region 获取正确的Id,如果不是正整数,返回0 
    ///  
    /// 获取正确的Id,如果不是正整数,返回0 
    ///  
    ///  
    /// 返回正确的整数ID,失败返回0 
    public static int StrToId(string _value) 
    { 
      if (IsNumberId(_value)) 
 return int.Parse(_value); 
      else 
 return 0; 
    } 
    #endregion 
    #region 检查一个字符串是否是纯数字构成的,一般用于查询字符串参数的有效性验证。 
    ///  
    /// 检查一个字符串是否是纯数字构成的,一般用于查询字符串参数的有效性验证。(0除外) 
    ///  
    /// 需验证的字符串。。 
    /// 是否合法的bool值。 
    public static bool IsNumberId(string _value) 
    { 
      return QuickValidate("^[1-9]*[0-9]*$", _value); 
    } 
    #endregion 
    #region 快速验证一个字符串是否符合指定的正则表达式。 
    ///  
    /// 快速验证一个字符串是否符合指定的正则表达式。 
    ///  
    /// 正则表达式的内容。 
    /// 需验证的字符串。 
    /// 是否合法的bool值。 
    public static bool QuickValidate(string _express, string _value) 
    { 
      if (_value == null) return false; 
      Regex myRegex = new Regex(_express); 
      if (_value.Length == 0) 
      { 
 return false; 
      } 
      return myRegex.IsMatch(_value); 
    } 
    #endregion 
 
 
    #region 根据配置对指定字符串进行 MD5 加密 
    ///  
    /// 根据配置对指定字符串进行 MD5 加密 
    ///  
    ///  
    ///  
    public static string GetMD5(string s) 
    { 
      //md5加密 
      s = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(s, "md5").ToString(); 
 
      return s.ToLower().Substring(8, 16); 
    } 
    #endregion 
 
    #region 得到字符串长度,一个汉字长度为2 
    ///  
    /// 得到字符串长度,一个汉字长度为2 
    ///  
    /// 参数字符串 
    ///  
    public static int StrLength(string inputString) 
    { 
      System.Text.ASCIIEncoding ascii = new System.Text.ASCIIEncoding(); 
      int tempLen = 0; 
      byte[] s = ascii.GetBytes(inputString); 
      for (int i = 0; i < s.Length; i++) 
      { 
 if ((int)s[i] == 63) 
   tempLen += 2; 
 else 
   tempLen += 1; 
      } 
      return tempLen; 
    } 
    #endregion 
 
    #region 截取指定长度字符串 
    ///  
    /// 截取指定长度字符串 
    ///  
    /// 要处理的字符串 
    /// 指定长度 
    /// 返回处理后的字符串 
    public static string ClipString(string inputString, int len) 
    { 
      bool isShowFix = false; 
      if (len % 2 == 1) 
      { 
 isShowFix = true; 
 len--; 
      } 
      System.Text.ASCIIEncoding ascii = new System.Text.ASCIIEncoding(); 
      int tempLen = 0; 
      string tempString = ""; 
      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 (isShowFix && mybyte.Length > len) 
 tempString += "…"; 
      return tempString; 
    } 
    #endregion 
 
 
 
    #region HTML转行成TEXT 
    ///  
    /// HTML转行成TEXT 
    ///  
    ///  
    ///  
    public static string HtmlToTxt(string strHtml) 
    { 
      string[] aryReg ={ 
      @"", 
      @"<(/s*)?!?((w+:)?w+)(w+(s*=?s*(([""'])(\[""'tbnr]|[^7])*?7|w+)|.{0})|s)*?(/s*)?>", 
      @"([rn])[s]+", 
      @"&(quot|#34);", 
      @"&(amp|#38);", 
      @"&(lt|#60);", 
      @"&(gt|#62);",  
      @"&(nbsp|#160);",  
      @"&(iexcl|#161);", 
      @"&(cent|#162);", 
      @"&(pound|#163);", 
      @"&(copy|#169);", 
      @"&#(d+);", 
      @"-->", 
      @"



我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号