StringExtension.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. using Alchemy.Core.Services;
  2. using System.Text.RegularExpressions;
  3. namespace Alchemy.Core.Extensions
  4. {
  5. public static class StringExtension
  6. {
  7. /// <summary>
  8. /// 字符串转Double
  9. /// </summary>
  10. /// <param name="strInput"></param>
  11. /// <returns></returns>
  12. public static double ToDouble(this string? strInput)
  13. {
  14. if (strInput.IsNull())
  15. return -1;
  16. bool result = double.TryParse(strInput, out double number);
  17. if (result)
  18. return number;
  19. else
  20. return -1;
  21. }
  22. /// <summary>
  23. /// 字符串转Long
  24. /// </summary>
  25. /// <param name="strInput"></param>
  26. /// <returns></returns>
  27. public static long ToLong(this string? strInput)
  28. {
  29. if (strInput.IsNull())
  30. return -1;
  31. bool result = long.TryParse(strInput, out long number);
  32. if (result)
  33. return number;
  34. else
  35. return -1;
  36. }
  37. /// <summary>
  38. /// 字符串转Int
  39. /// </summary>
  40. /// <param name="strInput"></param>
  41. /// <returns></returns>
  42. public static int ToInt(this string? strInput)
  43. {
  44. if (strInput.IsNull())
  45. return -1;
  46. bool result = int.TryParse(strInput, out int number);
  47. if (result)
  48. return number;
  49. else
  50. return -1;
  51. }
  52. /// <summary>
  53. /// 字符串转Float
  54. /// </summary>
  55. /// <param name="strInput"></param>
  56. /// <returns></returns>
  57. public static float ToFloat(this string? strInput)
  58. {
  59. if (strInput.IsNull())
  60. return -1;
  61. bool result = float.TryParse(strInput, out float number);
  62. if (result)
  63. return number;
  64. else
  65. return -1;
  66. }
  67. /// <summary>
  68. /// 字符串转布尔值
  69. /// </summary>
  70. /// <param name="strInput"></param>
  71. /// <returns></returns>
  72. public static bool ToBool(this string? strInput)
  73. {
  74. if (strInput.IsNull())
  75. return false;
  76. return bool.Parse(strInput);
  77. }
  78. /// <summary>
  79. /// 字符串转字节
  80. /// </summary>
  81. /// <param name="strInput"></param>
  82. /// <returns></returns>
  83. public static byte ToByte(this string strInput)
  84. {
  85. return Convert.ToByte(strInput);
  86. }
  87. /// <summary>
  88. /// 字符串转日期格式
  89. /// </summary>
  90. /// <param name="strDt"></param>
  91. /// <returns></returns>
  92. public static DateTime ToDt(this string strDt)
  93. {
  94. strDt = strDt.Replace("-0", "/");
  95. strDt = strDt.Replace("-", "/");
  96. return DateTime.Parse(strDt);
  97. }
  98. /// <summary>
  99. /// 字符串日期转时间戳
  100. /// </summary>
  101. /// <param name="strDateTime"></param>
  102. /// <returns></returns>
  103. public static long ToTimeStamp(this string strDateTime)
  104. {
  105. long timeStamp = 946656000;
  106. try
  107. {
  108. timeStamp = (Convert.ToDateTime(strDateTime).Ticks - TimeZoneInfo.ConvertTimeFromUtc(new DateTime(1970, 1, 1), TimeZoneInfo.Local).Ticks) / 10000000;
  109. }
  110. catch (Exception ex)
  111. {
  112. throw new Exception(ex.Message);
  113. }
  114. return timeStamp;
  115. }
  116. /// <summary>
  117. /// 字符串是否为空
  118. /// </summary>
  119. /// <param name="strParam"></param>
  120. /// <returns></returns>
  121. public static bool IsNull(this string? strParam)
  122. {
  123. return string.IsNullOrEmpty(strParam);
  124. }
  125. public static bool IsNotNull(this string? strParam)
  126. {
  127. return !string.IsNullOrEmpty(strParam);
  128. }
  129. /// <summary>
  130. /// 判断是否合规日期时间字符串
  131. /// </summary>
  132. /// <param name="strParam"></param>
  133. /// <returns></returns>
  134. public static bool IsDateTime(this string strParam)
  135. {
  136. if (strParam.IsNull())
  137. return false;
  138. return RegexService.IsMatch(strParam, @"^[1-9]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])\s+(20|21|22|23|[0-1]\d):[0-5]\d:[0-5]\d$");
  139. }
  140. /// <summary>
  141. /// 判断是否合规日期字符串
  142. /// </summary>
  143. /// <param name="strParam"></param>
  144. /// <returns></returns>
  145. public static bool IsDate(this string strParam)
  146. {
  147. if (strParam.IsNull())
  148. return false;
  149. return RegexService.IsMatch(strParam, @"^((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29))$");
  150. }
  151. /// <summary>
  152. /// 判断是否合规IP地址或域名
  153. /// </summary>
  154. /// <param name="strParam"></param>
  155. /// <returns></returns>
  156. public static bool IsIPorDomain(this string strParam)
  157. {
  158. if (strParam.IsNull())
  159. return false;
  160. bool value = strParam.IsIPv4();
  161. if (!value)
  162. {
  163. if (strParam.Contains("."))
  164. {
  165. value = true;
  166. }
  167. }
  168. return value;
  169. }
  170. /// <summary>
  171. /// 检测是否IP地址
  172. /// </summary>
  173. /// <param name="strIP"></param>
  174. /// <returns></returns>
  175. public static bool IsIPv4(this string strIP)
  176. {
  177. if (strIP.IsNull())
  178. return false;
  179. if (strIP.Length < 7 || strIP.Length > 15)
  180. return false;
  181. return RegexService.IsMatch(strIP, @"^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$");
  182. }
  183. /// <summary>
  184. /// 判断是否版本号
  185. /// </summary>
  186. /// <param name="strParam"></param>
  187. /// <returns></returns>
  188. public static bool IsVersion(this string strParam)
  189. {
  190. bool value = false;
  191. if (strParam.Contains("."))
  192. {
  193. string[] arrVer = strParam.Split('.');
  194. if (arrVer.Length > 1)
  195. {
  196. value = true;
  197. foreach (string strVerNum in arrVer)
  198. {
  199. value = value & int.TryParse(strVerNum, out int verNum);
  200. }
  201. }
  202. }
  203. return value;
  204. }
  205. /// <summary>
  206. /// 判断是否包含中文
  207. /// </summary>
  208. /// <param name="strParam"></param>
  209. /// <returns></returns>
  210. public static bool ContainsChinese(this string strParam)
  211. {
  212. return Regex.IsMatch(strParam, @"[\u4e00-\u9fa5]");
  213. }
  214. }
  215. }