[C#] C#自定义的字符串操作增强类 →→→→→进入此内容的聊天室

来自 , 2020-07-27, 写在 C#, 查看 108 次.
URL http://www.code666.cn/view/46aac8e8
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Text.RegularExpressions;
  5.  
  6. namespace DotNet.Utilities
  7. {
  8.     /// <summary>
  9.     /// 字符串操作类
  10.     /// 1、GetStrArray(string str, char speater, bool toLower)  把字符串按照分隔符转换成 List
  11.     /// 2、GetStrArray(string str) 把字符串转 按照, 分割 换为数据
  12.     /// 3、GetArrayStr(List list, string speater) 把 List 按照分隔符组装成 string
  13.     /// 4、GetArrayStr(List list)  得到数组列表以逗号分隔的字符串
  14.     /// 5、GetArrayValueStr(Dictionary<int, int> list)得到数组列表以逗号分隔的字符串
  15.     /// 6、DelLastComma(string str)删除最后结尾的一个逗号
  16.     /// 7、DelLastChar(string str, string strchar)删除最后结尾的指定字符后的字符
  17.     /// 8、ToSBC(string input)转全角的函数(SBC case)
  18.     /// 9、ToDBC(string input)转半角的函数(SBC case)
  19.     /// 10、GetSubStringList(string o_str, char sepeater)把字符串按照指定分隔符装成 List 去除重复
  20.     /// 11、GetCleanStyle(string StrList, string SplitString)将字符串样式转换为纯字符串
  21.     /// 12、GetNewStyle(string StrList, string NewStyle, string SplitString, out string Error)将字符串转换为新样式
  22.     /// 13、SplitMulti(string str, string splitstr)分割字符串
  23.     /// 14、SqlSafeString(string String, bool IsDel)
  24.     /// </summary>
  25.     public class StringPlus
  26.     {
  27.         /// <summary>
  28.         /// 把字符串按照分隔符转换成 List
  29.         /// </summary>
  30.         /// <param name="str">源字符串</param>
  31.         /// <param name="speater">分隔符</param>
  32.         /// <param name="toLower">是否转换为小写</param>
  33.         /// <returns></returns>
  34.         public static List<string> GetStrArray(string str, char speater, bool toLower)
  35.         {
  36.             List<string> list = new List<string>();
  37.             string[] ss = str.Split(speater);
  38.             foreach (string s in ss)
  39.             {
  40.                 if (!string.IsNullOrEmpty(s) && s != speater.ToString())
  41.                 {
  42.                     string strVal = s;
  43.                     if (toLower)
  44.                     {
  45.                         strVal = s.ToLower();
  46.                     }
  47.                     list.Add(strVal);
  48.                 }
  49.             }
  50.             return list;
  51.         }
  52.         /// <summary>
  53.         /// 把字符串转 按照, 分割 换为数据
  54.         /// </summary>
  55.         /// <param name="str"></param>
  56.         /// <returns></returns>
  57.         public static string[] GetStrArray(string str)
  58.         {
  59.             return str.Split(new Char[] { ',' });
  60.         }
  61.         /// <summary>
  62.         /// 把 List<string> 按照分隔符组装成 string
  63.         /// </summary>
  64.         /// <param name="list"></param>
  65.         /// <param name="speater"></param>
  66.         /// <returns></returns>
  67.         public static string GetArrayStr(List<string> list, string speater)
  68.         {
  69.             StringBuilder sb = new StringBuilder();
  70.             for (int i = 0; i < list.Count; i++)
  71.             {
  72.                 if (i == list.Count - 1)
  73.                 {
  74.                     sb.Append(list[i]);
  75.                 }
  76.                 else
  77.                 {
  78.                     sb.Append(list[i]);
  79.                     sb.Append(speater);
  80.                 }
  81.             }
  82.             return sb.ToString();
  83.         }
  84.         /// <summary>
  85.         /// 得到数组列表以逗号分隔的字符串
  86.         /// </summary>
  87.         /// <param name="list"></param>
  88.         /// <returns></returns>
  89.         public static string GetArrayStr(List<int> list)
  90.         {
  91.             StringBuilder sb = new StringBuilder();
  92.             for (int i = 0; i < list.Count; i++)
  93.             {
  94.                 if (i == list.Count - 1)
  95.                 {
  96.                     sb.Append(list[i].ToString());
  97.                 }
  98.                 else
  99.                 {
  100.                     sb.Append(list[i]);
  101.                     sb.Append(",");
  102.                 }
  103.             }
  104.             return sb.ToString();
  105.         }
  106.         /// <summary>
  107.         /// 得到数组列表以逗号分隔的字符串
  108.         /// </summary>
  109.         /// <param name="list"></param>
  110.         /// <returns></returns>
  111.         public static string GetArrayValueStr(Dictionary<int, int> list)
  112.         {
  113.             StringBuilder sb = new StringBuilder();
  114.             foreach (KeyValuePair<int, int> kvp in list)
  115.             {
  116.                 sb.Append(kvp.Value + ",");
  117.             }
  118.             if (list.Count > 0)
  119.             {
  120.                 return DelLastComma(sb.ToString());
  121.             }
  122.             else
  123.             {
  124.                 return "";
  125.             }
  126.         }
  127.  
  128.  
  129.         #region 删除最后一个字符之后的字符
  130.  
  131.         /// <summary>
  132.         /// 删除最后结尾的一个逗号
  133.         /// </summary>
  134.         public static string DelLastComma(string str)
  135.         {
  136.             return str.Substring(0, str.LastIndexOf(","));
  137.         }
  138.  
  139.         /// <summary>
  140.         /// 删除最后结尾的指定字符后的字符
  141.         /// </summary>
  142.         public static string DelLastChar(string str, string strchar)
  143.         {
  144.             return str.Substring(0, str.LastIndexOf(strchar));
  145.         }
  146.  
  147.         #endregion
  148.  
  149.  
  150.  
  151.  
  152.         /// <summary>
  153.         /// 转全角的函数(SBC case)
  154.         /// </summary>
  155.         /// <param name="input"></param>
  156.         /// <returns></returns>
  157.         public static string ToSBC(string input)
  158.         {
  159.             //半角转全角:
  160.             char[] c = input.ToCharArray();
  161.             for (int i = 0; i < c.Length; i++)
  162.             {
  163.                 if (c[i] == 32)
  164.                 {
  165.                     c[i] = (char)12288;
  166.                     continue;
  167.                 }
  168.                 if (c[i] < 127)
  169.                     c[i] = (char)(c[i] + 65248);
  170.             }
  171.             return new string(c);
  172.         }
  173.  
  174.         /// <summary>
  175.         ///  转半角的函数(SBC case)
  176.         /// </summary>
  177.         /// <param name="input">输入</param>
  178.         /// <returns></returns>
  179.         public static string ToDBC(string input)
  180.         {
  181.             char[] c = input.ToCharArray();
  182.             for (int i = 0; i < c.Length; i++)
  183.             {
  184.                 if (c[i] == 12288)
  185.                 {
  186.                     c[i] = (char)32;
  187.                     continue;
  188.                 }
  189.                 if (c[i] > 65280 && c[i] < 65375)
  190.                     c[i] = (char)(c[i] - 65248);
  191.             }
  192.             return new string(c);
  193.         }
  194.  
  195.         /// <summary>
  196.         /// 把字符串按照指定分隔符装成 List 去除重复
  197.         /// </summary>
  198.         /// <param name="o_str"></param>
  199.         /// <param name="sepeater"></param>
  200.         /// <returns></returns>
  201.         public static List<string> GetSubStringList(string o_str, char sepeater)
  202.         {
  203.             List<string> list = new List<string>();
  204.             string[] ss = o_str.Split(sepeater);
  205.             foreach (string s in ss)
  206.             {
  207.                 if (!string.IsNullOrEmpty(s) && s != sepeater.ToString())
  208.                 {
  209.                     list.Add(s);
  210.                 }
  211.             }
  212.             return list;
  213.         }
  214.  
  215.  
  216.         #region 将字符串样式转换为纯字符串
  217.         /// <summary>
  218.         ///  将字符串样式转换为纯字符串
  219.         /// </summary>
  220.         /// <param name="StrList"></param>
  221.         /// <param name="SplitString"></param>
  222.         /// <returns></returns>
  223.         public static string GetCleanStyle(string StrList, string SplitString)
  224.         {
  225.             string RetrunValue = "";
  226.             //如果为空,返回空值
  227.             if (StrList == null)
  228.             {
  229.                 RetrunValue = "";
  230.             }
  231.             else
  232.             {
  233.                 //返回去掉分隔符
  234.                 string NewString = "";
  235.                 NewString = StrList.Replace(SplitString, "");
  236.                 RetrunValue = NewString;
  237.             }
  238.             return RetrunValue;
  239.         }
  240.         #endregion
  241.  
  242.         #region 将字符串转换为新样式
  243.         /// <summary>
  244.         /// 将字符串转换为新样式
  245.         /// </summary>
  246.         /// <param name="StrList"></param>
  247.         /// <param name="NewStyle"></param>
  248.         /// <param name="SplitString"></param>
  249.         /// <param name="Error"></param>
  250.         /// <returns></returns>
  251.         public static string GetNewStyle(string StrList, string NewStyle, string SplitString, out string Error)
  252.         {
  253.             string ReturnValue = "";
  254.             //如果输入空值,返回空,并给出错误提示
  255.             if (StrList == null)
  256.             {
  257.                 ReturnValue = "";
  258.                 Error = "请输入需要划分格式的字符串";
  259.             }
  260.             else
  261.             {
  262.                 //检查传入的字符串长度和样式是否匹配,如果不匹配,则说明使用错误。给出错误信息并返回空值
  263.                 int strListLength = StrList.Length;
  264.                 int NewStyleLength = GetCleanStyle(NewStyle, SplitString).Length;
  265.                 if (strListLength != NewStyleLength)
  266.                 {
  267.                     ReturnValue = "";
  268.                     Error = "样式格式的长度与输入的字符长度不符,请重新输入";
  269.                 }
  270.                 else
  271.                 {
  272.                     //检查新样式中分隔符的位置
  273.                     string Lengstr = "";
  274.                     for (int i = 0; i < NewStyle.Length; i++)
  275.                     {
  276.                         if (NewStyle.Substring(i, 1) == SplitString)
  277.                         {
  278.                             Lengstr = Lengstr + "," + i;
  279.                         }
  280.                     }
  281.                     if (Lengstr != "")
  282.                     {
  283.                         Lengstr = Lengstr.Substring(1);
  284.                     }
  285.                     //将分隔符放在新样式中的位置
  286.                     string[] str = Lengstr.Split(',');
  287.                     foreach (string bb in str)
  288.                     {
  289.                         StrList = StrList.Insert(int.Parse(bb), SplitString);
  290.                     }
  291.                     //给出最后的结果
  292.                     ReturnValue = StrList;
  293.                     //因为是正常的输出,没有错误
  294.                     Error = "";
  295.                 }
  296.             }
  297.             return ReturnValue;
  298.         }
  299.         #endregion
  300.  
  301.         /// <summary>
  302.         /// 分割字符串
  303.         /// </summary>
  304.         /// <param name="str"></param>
  305.         /// <param name="splitstr"></param>
  306.         /// <returns></returns>
  307.         public static string[] SplitMulti(string str, string splitstr)
  308.         {
  309.             string[] strArray = null;
  310.             if ((str != null) && (str != ""))
  311.             {
  312.                 strArray = new Regex(splitstr).Split(str);
  313.             }
  314.             return strArray;
  315.         }
  316.         public static string SqlSafeString(string String, bool IsDel)
  317.         {
  318.             if (IsDel)
  319.             {
  320.                 String = String.Replace("'", "");
  321.                 String = String.Replace("\"", "");
  322.                 return String;
  323.             }
  324.             String = String.Replace("'", "&#39;");
  325.             String = String.Replace("\"", """);
  326.            return String;
  327.        }
  328.  
  329.        #region 获取正确的Id,如果不是正整数,返回0
  330.        /// <summary>
  331.        /// 获取正确的Id,如果不是正整数,返回0
  332.        /// </summary>
  333.        /// <param name="_value"></param>
  334.        /// <returns>返回正确的整数ID,失败返回0</returns>
  335.        public static int StrToId(string _value)
  336.        {
  337.            if (IsNumberId(_value))
  338.                return int.Parse(_value);
  339.            else
  340.                return 0;
  341.        }
  342.        #endregion
  343.        #region 检查一个字符串是否是纯数字构成的,一般用于查询字符串参数的有效性验证。
  344.        /// <summary>
  345.        /// 检查一个字符串是否是纯数字构成的,一般用于查询字符串参数的有效性验证。(0除外)
  346.        /// </summary>
  347.        /// <param name="_value">需验证的字符串。。</param>
  348.        /// <returns>是否合法的bool值。</returns>
  349.        public static bool IsNumberId(string _value)
  350.        {
  351.            return QuickValidate("^[1-9]*[0-9]*$", _value);
  352.        }
  353.        #endregion
  354.        #region 快速验证一个字符串是否符合指定的正则表达式。
  355.        /// <summary>
  356.        /// 快速验证一个字符串是否符合指定的正则表达式。
  357.        /// </summary>
  358.        /// <param name="_express">正则表达式的内容。</param>
  359.        /// <param name="_value">需验证的字符串。</param>
  360.        /// <returns>是否合法的bool值。</returns>
  361.        public static bool QuickValidate(string _express, string _value)
  362.        {
  363.            if (_value == null) return false;
  364.            Regex myRegex = new Regex(_express);
  365.            if (_value.Length == 0)
  366.            {
  367.                return false;
  368.            }
  369.            return myRegex.IsMatch(_value);
  370.        }
  371.        #endregion
  372.    }
  373. }
  374.  
  375. //csharp/8587

回复 "C#自定义的字符串操作增强类"

这儿你可以回复上面这条便签

captcha