StringHelper.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. namespace Common
  7. {
  8. /// <summary>
  9. /// 字符串操作 - 工具方法
  10. /// </summary>
  11. public static partial class StringHelper
  12. {
  13. #region ContainsChinese(是否包含中文)
  14. /// <summary>
  15. /// 是否包含中文
  16. /// </summary>
  17. /// <param name="text">文本</param>
  18. public static bool ContainsChinese(string text)
  19. {
  20. const string pattern = "[\u4e00-\u9fa5]+";
  21. return Regex.IsMatch(text, pattern);
  22. }
  23. #endregion
  24. #region ContainsNumber(是否包含数字)
  25. /// <summary>
  26. /// 是否包含数字
  27. /// </summary>
  28. /// <param name="text">文本</param>
  29. public static bool ContainsNumber(string text)
  30. {
  31. const string pattern = "[0-9]+";
  32. return Regex.IsMatch(text, pattern);
  33. }
  34. #endregion
  35. #region Distinct(去除重复)
  36. /// <summary>
  37. /// 去除重复
  38. /// </summary>
  39. /// <param name="value">值,范例1:"5555",返回"5",范例2:"4545",返回"45"</param>
  40. public static string Distinct(string value)
  41. {
  42. var array = value.ToCharArray();
  43. return new string(array.Distinct().ToArray());
  44. }
  45. #endregion
  46. #region 删除最后一个字符之后的字符
  47. /// <summary>
  48. /// 删除最后结尾的一个逗号
  49. /// </summary>
  50. public static string DelLastComma(string str)
  51. {
  52. return str.Substring(0, str.LastIndexOf(",", StringComparison.Ordinal));
  53. }
  54. /// <summary>
  55. /// 删除最后结尾的指定字符后的字符
  56. /// </summary>
  57. public static string DelLastChar(string str, string strchar)
  58. {
  59. return str.Substring(0, str.LastIndexOf(strchar, StringComparison.Ordinal));
  60. }
  61. /// <summary>
  62. /// 删除最后结尾的长度
  63. /// </summary>
  64. /// <param name="str"></param>
  65. /// <param name="Length"></param>
  66. /// <returns></returns>
  67. public static string DelLastLength(string str, int Length)
  68. {
  69. if (string.IsNullOrEmpty(str))
  70. return "";
  71. str = str.Substring(0, str.Length - Length);
  72. return str;
  73. }
  74. #endregion
  75. #region 快速验证一个字符串是否符合指定的正则表达式
  76. /// <summary>
  77. /// 快速验证一个字符串是否符合指定的正则表达式。
  78. /// </summary>
  79. /// <param name="express">正则表达式的内容。</param>
  80. /// <param name="value">需验证的字符串。</param>
  81. /// <returns>是否合法的bool值。</returns>
  82. public static bool QuickValidate(string express, string value)
  83. {
  84. if (value == null) return false;
  85. Regex myRegex = new Regex(express);
  86. if (value.Length == 0)
  87. {
  88. return false;
  89. }
  90. return myRegex.IsMatch(value);
  91. }
  92. #endregion
  93. #region 彩票类型参数首字母转换大小写
  94. /// <summary>
  95. /// 彩票类型参数首字母转换大小写
  96. /// </summary>
  97. /// <param name="value">字符串</param>
  98. /// <param name="c">分割字符</param>
  99. /// <param name="i">0:小写,1:大写</param>
  100. /// <returns></returns>
  101. public static string ParamTransUperOrLower(string value, char c, int i = 0)
  102. {
  103. string result = "";
  104. string[] arr = value.Split(c);
  105. for (int j = 0; j < arr.Length - 1; j++)
  106. {
  107. if (i == 0)
  108. {
  109. result += arr[j].ToLower() + c;
  110. }
  111. else
  112. {
  113. result += arr[j].ToUpper() + c;
  114. }
  115. }
  116. result += arr[arr.Length - 1];
  117. return result;
  118. }
  119. /// <summary>
  120. /// 开奖日期转换
  121. /// </summary>
  122. /// <param name="desc"></param>
  123. /// <returns></returns>
  124. public static string getKjDesc(string desc)
  125. {
  126. string time = "日一二三四五六";
  127. string[] txt = desc.Split(',');
  128. try
  129. {
  130. if (txt.Length == 7)
  131. {
  132. desc = "每天开奖一次";
  133. }
  134. else
  135. {
  136. desc = "每";
  137. for (var i = 0; i < txt.Length; i++)
  138. {
  139. desc += "周" + time.ElementAt(Int32.Parse(txt[i]));
  140. if (i < txt.Length - 1)
  141. {
  142. desc = desc + "、";
  143. }
  144. }
  145. }
  146. }
  147. catch (Exception e)
  148. {
  149. return "";
  150. }
  151. return desc;
  152. }
  153. /// <summary>
  154. /// 获取兑奖截止时间
  155. /// </summary>
  156. /// <returns></returns>
  157. public static DateTime GetDuiJiangtime(string OpenTime)
  158. {
  159. try
  160. {
  161. DateTime tmp = new DateTime();
  162. DateTime.TryParse(OpenTime, out tmp);
  163. return tmp.AddDays(60);
  164. }
  165. catch (Exception ee)
  166. {
  167. return DateTime.Now.AddDays(60);
  168. }
  169. }
  170. /// <summary>
  171. /// 获取彩种去年的最后一期
  172. /// </summary>
  173. /// <param name="KJTime"></param>
  174. /// <returns></returns>
  175. public static int GetLastTermOfLastYear(string KJTime)
  176. {
  177. try
  178. {
  179. var kjtime = KJTime.Split(',');
  180. DateTime time = DateTime.Now;
  181. int year = time.Year - 1;
  182. DateTime etime = new DateTime(year, 12, 31);
  183. DateTime stime = new DateTime(year, 1, 1);
  184. var tp = etime - stime;
  185. int caday = tp.Days;
  186. int termcount = 0;
  187. for (int i = 0; i < caday; i++)
  188. {
  189. stime = stime.AddDays(1);
  190. if (kjtime.Contains(((int)stime.DayOfWeek).ToString()))
  191. {
  192. termcount += 1;
  193. }
  194. }
  195. return termcount;
  196. }
  197. catch (Exception ee)
  198. {
  199. return 0;
  200. }
  201. }
  202. public static int GetLastYear(string qs)
  203. {
  204. int lastyear = 0;
  205. try
  206. {
  207. if (qs.Length == 7)
  208. {
  209. lastyear = int.Parse(qs.Substring(0, 4)) - 1;
  210. }
  211. else
  212. {
  213. lastyear = int.Parse("20" + qs.Substring(0, 2)) - 1;
  214. }
  215. return lastyear;
  216. }
  217. catch (Exception ee)
  218. {
  219. return 0;
  220. }
  221. }
  222. #endregion
  223. }
  224. }