StringExtension.cs 7.1 KB

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