StringExtensions.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. using System.Security.Cryptography;
  2. using System.Text;
  3. using System.Text.RegularExpressions;
  4. namespace Long.Core.Extensions;
  5. public static class StringExtensions
  6. {
  7. public static bool IsNullOrEmpty(this string str)
  8. {
  9. return string.IsNullOrEmpty(str);
  10. }
  11. public static bool IsNullOrWhiteSpace(this string str)
  12. {
  13. return string.IsNullOrWhiteSpace(str);
  14. }
  15. /// <summary>
  16. /// 断言空值
  17. /// </summary>
  18. /// <param name="str"></param>
  19. /// <exception cref="ArgumentNullException"></exception>
  20. public static void AssertNullOrEmpty(this string str, string v)
  21. {
  22. if (string.IsNullOrEmpty(str))
  23. {
  24. throw new ArgumentNullException($"参数 {nameof(str)}:{v}");
  25. }
  26. }
  27. /// <summary>
  28. /// 断言空值或空白字符串
  29. /// </summary>
  30. /// <param name="str"></param>
  31. /// <exception cref="ArgumentNullException"></exception>
  32. public static void AssertNullOrWhiteSpace(this string str, string v)
  33. {
  34. if (string.IsNullOrWhiteSpace(str))
  35. {
  36. throw new ArgumentNullException($"参数 {nameof(str)}:{v}");
  37. }
  38. }
  39. public static T ConvertTo<T>(this string source) where T : struct
  40. {
  41. return (T)Convert.ChangeType(source, typeof(T));
  42. }
  43. public static string Left(this string str, int len)
  44. {
  45. if (str == null)
  46. {
  47. throw new ArgumentNullException("str");
  48. }
  49. if (str.Length < len)
  50. {
  51. throw new ArgumentException("len argument can not be bigger than given string's length!");
  52. }
  53. return str.Substring(0, len);
  54. }
  55. public static string NormalizeLineEndings(this string str)
  56. {
  57. return str.Replace("\r\n", "\n").Replace("\r", "\n").Replace("\n", Environment.NewLine);
  58. }
  59. public static int NthIndexOf(this string str, char c, int n)
  60. {
  61. if (str == null)
  62. {
  63. throw new ArgumentNullException("str");
  64. }
  65. int num = 0;
  66. for (int i = 0; i < str.Length; i++)
  67. {
  68. if (str[i] == c && ++num == n)
  69. {
  70. return i;
  71. }
  72. }
  73. return -1;
  74. }
  75. public static string RemovePostFix(this string str, params string[] postFixes)
  76. {
  77. if (str == null || str == string.Empty)
  78. {
  79. return string.Empty;
  80. }
  81. if (postFixes.IsNullOrEmpty())
  82. {
  83. return str;
  84. }
  85. foreach (string text in postFixes)
  86. {
  87. if (str.EndsWith(text))
  88. {
  89. return str.Left(str.Length - text.Length);
  90. }
  91. }
  92. return str;
  93. }
  94. public static string RemovePreFix(this string str, params string[] preFixes)
  95. {
  96. if (str == null || str == string.Empty)
  97. {
  98. return string.Empty;
  99. }
  100. if (preFixes.IsNullOrEmpty())
  101. {
  102. return str;
  103. }
  104. foreach (string text in preFixes)
  105. {
  106. if (str.StartsWith(text))
  107. {
  108. return str.Right(str.Length - text.Length);
  109. }
  110. }
  111. return str;
  112. }
  113. public static string Right(this string str, int len)
  114. {
  115. if (str == null)
  116. {
  117. throw new ArgumentNullException("str");
  118. }
  119. if (str.Length < len)
  120. {
  121. throw new ArgumentException("len argument can not be bigger than given string's length!");
  122. }
  123. return str.Substring(str.Length - len, len);
  124. }
  125. public static string[] Split(this string str, string separator)
  126. {
  127. return str.Split(new string[1] { separator }, StringSplitOptions.None);
  128. }
  129. public static string[] Split(this string str, string separator, StringSplitOptions options)
  130. {
  131. return str.Split(new string[1] { separator }, options);
  132. }
  133. public static string[] SplitToLines(this string str)
  134. {
  135. return Split(str, Environment.NewLine);
  136. }
  137. public static string[] SplitToLines(this string str, StringSplitOptions options)
  138. {
  139. return Split(str, Environment.NewLine, options);
  140. }
  141. public static T ToEnum<T>(this string value) where T : struct
  142. {
  143. if (value == null)
  144. {
  145. throw new ArgumentNullException("value");
  146. }
  147. return (T)Enum.Parse(typeof(T), value);
  148. }
  149. public static T ToEnum<T>(this string value, bool ignoreCase) where T : struct
  150. {
  151. if (value == null)
  152. {
  153. throw new ArgumentNullException("value");
  154. }
  155. return (T)Enum.Parse(typeof(T), value, ignoreCase);
  156. }
  157. public static string ToMd5(this string str)
  158. {
  159. using MD5 mD = MD5.Create();
  160. byte[] bytes = Encoding.UTF8.GetBytes(str);
  161. byte[] array = mD.ComputeHash(bytes);
  162. StringBuilder stringBuilder = new StringBuilder();
  163. byte[] array2 = array;
  164. foreach (byte b in array2)
  165. {
  166. stringBuilder.Append(b.ToString("X2"));
  167. }
  168. return stringBuilder.ToString();
  169. }
  170. #region 新增
  171. /// <summary>
  172. /// 是否是电子邮箱
  173. /// </summary>
  174. /// <param name="str"></param>
  175. /// <returns></returns>
  176. public static bool IsEmail(this string str)
  177. {
  178. var reg = new Regex("^[a-zA-Z0-9.!#$%&'*+\\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$");
  179. return reg.IsMatch(str);
  180. }
  181. /// <summary>
  182. /// 是否是电话
  183. /// </summary>
  184. /// <param name="str"></param>
  185. /// <returns></returns>
  186. public static bool IsPhone(this string str)
  187. {
  188. var reg = new Regex("^1(3|4|5|7|8|9)\\d{9}$");
  189. return reg.IsMatch(str);
  190. }
  191. /// <summary>
  192. /// 是否是密码
  193. /// </summary>
  194. /// <param name="str"></param>
  195. /// <returns></returns>
  196. public static bool IsPassword(this string str)
  197. {
  198. var reg = new Regex("/^([a-z]|[A-Z]|[0-9]|[_-]){6,128}$");
  199. return reg.IsMatch(str);
  200. }
  201. /// <summary>
  202. /// 是否是 HTML
  203. /// </summary>
  204. /// <param name="str"></param>
  205. /// <returns></returns>
  206. public static bool IsHTML(this string str)
  207. {
  208. return str.Length >= 3 && str.Substring(0, 1) == "<" && str.Substring(str.Length - 2, 1) == ">";
  209. }
  210. /// <summary>
  211. /// 是否是 XML
  212. /// </summary>
  213. /// <param name="str"></param>
  214. /// <returns></returns>
  215. public static bool IsXML(this string str)
  216. {
  217. return str.IsHTML() && (str.IndexOf("<?xml") == 0 || str.IndexOf("<!DOCTYPE") == 0);
  218. }
  219. /// <summary>
  220. /// 是否是 JSON 字符串
  221. /// </summary>
  222. /// <param name="str"></param>
  223. /// <returns></returns>
  224. public static bool IsJSON(this string str)
  225. {
  226. var start = str.Substring(0, 1);
  227. var end = str.Substring(str.Length - 1, 1);
  228. return (start == "{" || start == "[") && (end == "}" || end == "]");
  229. }
  230. #endregion
  231. }