StringPlus.cs 12 KB

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