stringHelper类

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 "";
             }
         }
     /// <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;
         }
         /// <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("'", "&#39;");
             String = String.Replace("\"", "&#34;");
             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

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值