123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373 |
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Text.RegularExpressions;
- namespace Ant.Service.Utilities
- {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public class StringPlus
- {
-
-
-
-
-
-
-
- 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;
- }
-
-
-
-
-
- public static string[] GetStrArray(string str)
- {
- return str.Split(new Char[] { ',' });
- }
-
-
-
-
-
-
- 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();
- }
-
-
-
-
-
- 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();
- }
-
-
-
-
-
- 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 删除最后一个字符之后的字符
-
-
-
- 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
-
-
-
-
-
- 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);
- }
-
-
-
-
-
- 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);
- }
-
-
-
-
-
-
- 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 将字符串样式转换为纯字符串
-
-
-
-
-
-
- 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
-
-
-
-
-
- public static int StrToId(string _value)
- {
- if (IsNumberId(_value))
- return int.Parse(_value);
- else
- return 0;
- }
- #endregion
- #region 检查一个字符串是否是纯数字构成的,一般用于查询字符串参数的有效性验证。
-
-
-
-
-
- public static bool IsNumberId(string _value)
- {
- return QuickValidate("^[1-9]*[0-9]*$", _value);
- }
- #endregion
- #region 快速验证一个字符串是否符合指定的正则表达式。
-
-
-
-
-
-
- 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
- }
- }
|