欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-12-04 14:26:40
一个非常好的c#字符串操作处理类stringhelper.cs,具体内容如下 /// /// 类说明:assistant...

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

/// <summary> 
/// 类说明:assistant 
/// 编 码 人:苏飞 
/// 联系方式:361983679  
/// 更新网站:http://www.sufeinet.com/thread-655-1-1.html 
/// </summary> 
using system; 
using system.collections.generic; 
using system.text; 
using system.text.regularexpressions; 
 
namespace dotnet.utilities 
{ 
  /// <summary> 
  /// 字符串操作类 
  /// 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<int, int> 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) 
  /// </summary> 
  public class stringhelper 
  { 
    /// <summary> 
    /// 把字符串按照分隔符转换成 list 
    /// </summary> 
    /// <param name="str">源字符串</param> 
    /// <param name="speater">分隔符</param> 
    /// <param name="tolower">是否转换为小写</param> 
    /// <returns></returns> 
    public static list<string> getstrarray(string str, char speater, bool tolower) 
    { 
      list<string> list = new list<string>(); 
      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; 
    } 
    /// <summary> 
    /// 把字符串转 按照, 分割 换为数据 
    /// </summary> 
    /// <param name="str"></param> 
    /// <returns></returns> 
    public static string[] getstrarray(string str) 
    { 
      return str.split(new char[] { ',' }); 
    } 
    /// <summary> 
    /// 把 list<string> 按照分隔符组装成 string 
    /// </summary> 
    /// <param name="list"></param> 
    /// <param name="speater"></param> 
    /// <returns></returns> 
    public static string getarraystr(list<string> 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(); 
    } 
    /// <summary> 
    /// 得到数组列表以逗号分隔的字符串 
    /// </summary> 
    /// <param name="list"></param> 
    /// <returns></returns> 
    public static string getarraystr(list<int> 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(); 
    } 
    /// <summary> 
    /// 得到数组列表以逗号分隔的字符串 
    /// </summary> 
    /// <param name="list"></param> 
    /// <returns></returns> 
    public static string getarrayvaluestr(dictionary<int, int> list) 
    { 
      stringbuilder sb = new stringbuilder(); 
      foreach (keyvaluepair<int, int> kvp in list) 
      { 
        sb.append(kvp.value + ","); 
      } 
      if (list.count > 0) 
      { 
        return dellastcomma(sb.tostring()); 
      } 
      else 
      { 
        return ""; 
      } 
    } 
 
    #region 删除最后一个字符之后的字符 
 
    /// <summary> 
    /// 删除最后结尾的一个逗号 
    /// </summary> 
    public static string dellastcomma(string str) 
    { 
      return str.substring(0, str.lastindexof(",")); 
    } 
 
    /// <summary> 
    /// 删除最后结尾的指定字符后的字符 
    /// </summary> 
    public static string dellastchar(string str, string strchar) 
    { 
      return str.substring(0, str.lastindexof(strchar)); 
    } 
 
    #endregion 
 
    /// <summary> 
    /// 转全角的函数(sbc case) 
    /// </summary> 
    /// <param name="input"></param> 
    /// <returns></returns> 
    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); 
    } 
 
    /// <summary> 
    /// 转半角的函数(sbc case) 
    /// </summary> 
    /// <param name="input">输入</param> 
    /// <returns></returns> 
    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); 
    } 
 
    /// <summary> 
    /// 把字符串按照指定分隔符装成 list 去除重复 
    /// </summary> 
    /// <param name="o_str"></param> 
    /// <param name="sepeater"></param> 
    /// <returns></returns> 
    public static list<string> getsubstringlist(string o_str, char sepeater) 
    { 
      list<string> list = new list<string>(); 
      string[] ss = o_str.split(sepeater); 
      foreach (string s in ss) 
      { 
        if (!string.isnullorempty(s) && s != sepeater.tostring()) 
        { 
          list.add(s); 
        } 
      } 
      return list; 
    } 
 
 
    #region 将字符串样式转换为纯字符串 
    /// <summary> 
    /// 将字符串样式转换为纯字符串 
    /// </summary> 
    /// <param name="strlist"></param> 
    /// <param name="splitstring"></param> 
    /// <returns></returns> 
    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 将字符串转换为新样式 
    /// <summary> 
    /// 将字符串转换为新样式 
    /// </summary> 
    /// <param name="strlist"></param> 
    /// <param name="newstyle"></param> 
    /// <param name="splitstring"></param> 
    /// <param name="error"></param> 
    /// <returns></returns> 
    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 
 
    /// <summary> 
    /// 分割字符串 
    /// </summary> 
    /// <param name="str"></param> 
    /// <param name="splitstr"></param> 
    /// <returns></returns> 
    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 
    /// <summary> 
    /// 获取正确的id,如果不是正整数,返回0 
    /// </summary> 
    /// <param name="_value"></param> 
    /// <returns>返回正确的整数id,失败返回0</returns> 
    public static int strtoid(string _value) 
    { 
      if (isnumberid(_value)) 
        return int.parse(_value); 
      else 
        return 0; 
    } 
    #endregion 
    #region 检查一个字符串是否是纯数字构成的,一般用于查询字符串参数的有效性验证。 
    /// <summary> 
    /// 检查一个字符串是否是纯数字构成的,一般用于查询字符串参数的有效性验证。(0除外) 
    /// </summary> 
    /// <param name="_value">需验证的字符串。。</param> 
    /// <returns>是否合法的bool值。</returns> 
    public static bool isnumberid(string _value) 
    { 
      return quickvalidate("^[1-9]*[0-9]*$", _value); 
    } 
    #endregion 
    #region 快速验证一个字符串是否符合指定的正则表达式。 
    /// <summary> 
    /// 快速验证一个字符串是否符合指定的正则表达式。 
    /// </summary> 
    /// <param name="_express">正则表达式的内容。</param> 
    /// <param name="_value">需验证的字符串。</param> 
    /// <returns>是否合法的bool值。</returns> 
    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 加密 
    /// <summary> 
    /// 根据配置对指定字符串进行 md5 加密 
    /// </summary> 
    /// <param name="s"></param> 
    /// <returns></returns> 
    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 
    /// <summary> 
    /// 得到字符串长度,一个汉字长度为2 
    /// </summary> 
    /// <param name="inputstring">参数字符串</param> 
    /// <returns></returns> 
    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 截取指定长度字符串 
    /// <summary> 
    /// 截取指定长度字符串 
    /// </summary> 
    /// <param name="inputstring">要处理的字符串</param> 
    /// <param name="len">指定长度</param> 
    /// <returns>返回处理后的字符串</returns> 
    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 
    /// <summary> 
    /// html转行成text 
    /// </summary> 
    /// <param name="strhtml"></param> 
    /// <returns></returns> 
    public static string htmltotxt(string strhtml) 
    { 
      string[] aryreg ={ 
      @"<script[^>]*?>.*?</script>", 
      @"<(\/\s*)?!?((\w+:)?\w+)(\w+(\s*=?\s*(([""'])(\\[""'tbnr]|[^\7])*?\7|\w+)|.{0})|\s)*?(\/\s*)?>", 
      @"([\r\n])[\s]+", 
      @"&(quot|#34);", 
      @"&(amp|#38);", 
      @"&(lt|#60);", 
      @"&(gt|#62);",  
      @"&(nbsp|#160);",  
      @"&(iexcl|#161);", 
      @"&(cent|#162);", 
      @"&(pound|#163);", 
      @"&(copy|#169);", 
      @"(\d+);", 
      @"-->", 
      @"<!--.*\n" 
      }; 
 
      string newreg = aryreg[0]; 
      string stroutput = strhtml; 
      for (int i = 0; i < aryreg.length; i++) 
      { 
        regex regex = new regex(aryreg[i], regexoptions.ignorecase); 
        stroutput = regex.replace(stroutput, string.empty); 
      } 
 
      stroutput.replace("<", ""); 
      stroutput.replace(">", ""); 
      stroutput.replace("\r\n", ""); 
 
 
      return stroutput; 
    } 
    #endregion 
 
    #region 判断对象是否为空 
    /// <summary> 
    /// 判断对象是否为空,为空返回true 
    /// </summary> 
    /// <typeparam name="t">要验证的对象的类型</typeparam> 
    /// <param name="data">要验证的对象</param>     
    public static bool isnullorempty<t>(t data) 
    { 
      //如果为null 
      if (data == null) 
      { 
        return true; 
      } 
 
      //如果为"" 
      if (data.gettype() == typeof(string)) 
      { 
        if (string.isnullorempty(data.tostring().trim())) 
        { 
          return true; 
        } 
      } 
 
      //如果为dbnull 
      if (data.gettype() == typeof(dbnull)) 
      { 
        return true; 
      } 
 
      //不为空 
      return false; 
    } 
 
    /// <summary> 
    /// 判断对象是否为空,为空返回true 
    /// </summary> 
    /// <param name="data">要验证的对象</param> 
    public static bool isnullorempty(object data) 
    { 
      //如果为null 
      if (data == null) 
      { 
        return true; 
      } 
 
      //如果为"" 
      if (data.gettype() == typeof(string)) 
      { 
        if (string.isnullorempty(data.tostring().trim())) 
        { 
          return true; 
        } 
      } 
 
      //如果为dbnull 
      if (data.gettype() == typeof(dbnull)) 
      { 
        return true; 
      } 
 
      //不为空 
      return false; 
    } 
    #endregion 
  } 
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。