StringExtension.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using System.Diagnostics.CodeAnalysis;
  2. using System.Text;
  3. using System.Text.RegularExpressions;
  4. namespace System
  5. {
  6. /// <summary>
  7. /// 字符串扩展方法
  8. /// </summary>
  9. public static class StringExtension
  10. {
  11. /// <summary>
  12. /// 判断字符串是否为 Null 或空字符串
  13. /// </summary>
  14. /// <param name="this"> 当前字符串 </param>
  15. /// <returns>如果字符串为 Null 或空字符串,则返回 true;否则返回 false.</returns>
  16. public static bool IsNullOrEmpty(this string? @this) => string.IsNullOrEmpty(@this);
  17. /// <summary>
  18. /// 判断字符串是否为 Null 或白字符串
  19. /// </summary>
  20. /// <param name="this"> 当前字符串 </param>
  21. /// <returns>如果字符串为 Null 或由空白字符组成,则返回 true;否则返回 false.</returns>
  22. /// <remarks>例如" "此时返回True</remarks>
  23. public static bool IsNullOrWhiteSpace(this string? @this) => string.IsNullOrWhiteSpace(@this);
  24. /// <summary>
  25. /// 判断字符串是否符合指定模式
  26. /// </summary>
  27. /// <param name="this"> 当前字符串 </param>
  28. /// <param name="pattern">要使用的模式.使用"*"作为通配符字符串.</param>
  29. /// <returns>如果字符串符合指定模式,则返回true; 否则返回false</returns>
  30. public static bool IsLike([NotNull] this string @this, string pattern)
  31. {
  32. // 将模式转换为正则表达式,并使用 ^$ 匹配整个字符串
  33. var regexPattern = "^" + Regex.Escape(pattern) + "$";
  34. // 转义特殊字符 ?、#、*、[] 和 [!]
  35. regexPattern = regexPattern.Replace(@"\[!", "[^")
  36. .Replace(@"\[", "[")
  37. .Replace(@"\]", "]")
  38. .Replace(@"\?", ".")
  39. .Replace(@"\*", ".*")
  40. .Replace(@"\#", @"\d");
  41. // 判断字符串是否符合正则表达式规则
  42. return Regex.IsMatch(@this, regexPattern);
  43. }
  44. /// <summary>
  45. /// 重复指定的字符串指定次数
  46. /// </summary>
  47. /// <param name="this"> 当前字符串 </param>
  48. /// <param name="repeatCount"> 重复次数 </param>
  49. /// <returns> 重复指定次数后的字符串 </returns>
  50. public static string Repeat([NotNull] this string @this, int repeatCount)
  51. {
  52. if (@this.Length == 1)
  53. {
  54. return new string(@this[0], repeatCount);
  55. }
  56. var sb = new StringBuilder(repeatCount * @this.Length);
  57. while (repeatCount-- > 0)
  58. {
  59. sb.Append(@this);
  60. }
  61. return sb.ToString();
  62. }
  63. /// <summary>
  64. /// 反转给定字符串
  65. /// </summary>
  66. /// <param name="this"> 当前字符串 </param>
  67. /// <returns> 反转后的字符串 </returns>
  68. public static string Reverse([NotNull] this string @this)
  69. {
  70. if (@this.Length <= 1)
  71. {
  72. return @this;
  73. }
  74. var chars = @this.ToCharArray();
  75. Array.Reverse(chars);
  76. return new string(chars);
  77. }
  78. /// <summary>
  79. /// 返回使用指定编码的字符串所表示的字节数组
  80. /// </summary>
  81. /// <param name="str"> 要转换的字符串 </param>
  82. /// <param name="encoding"> 要使用的编码 </param>
  83. /// <returns> 包含字符串中字符所表示的字节的数组 </returns>
  84. public static byte[] GetBytes([NotNull] this string str, Encoding encoding) => encoding.GetBytes(str);
  85. /// <summary>
  86. /// 判断两个字符串是否相等,忽略大小写.
  87. /// </summary>
  88. /// <param name="s1"> 字符串1 </param>
  89. /// <param name="s2"> 字符串2 </param>
  90. /// <returns>如果两个字符串相等(忽略大小写),则返回 true;否则返回 false.</returns>
  91. public static bool EqualsIgnoreCase(this string s1, string s2) => string.Equals(s1, s2, StringComparison.OrdinalIgnoreCase);
  92. /// <summary>
  93. /// 将字符串转换为长整型数值.
  94. /// </summary>
  95. /// <param name="txt"> 要转换的字符串 </param>
  96. /// <returns> 如果转换成功,返回转换后的长整型数值;否则返回 null. </returns>
  97. public static int? ToInt(this string @this)
  98. {
  99. bool status = int.TryParse(@this, out int result);
  100. if (status)
  101. return result;
  102. else
  103. return null;
  104. }
  105. /// <summary>
  106. /// 将字符串转换为长整型数值.
  107. /// </summary>
  108. /// <param name="txt"> 要转换的字符串 </param>
  109. /// <returns> 如果转换成功,返回转换后的长整型数值;否则返回 null. </returns>
  110. public static long? ToLong(this string @this)
  111. {
  112. bool status = long.TryParse(@this, out long result);
  113. if (status)
  114. return result;
  115. else
  116. return null;
  117. }
  118. /// <summary>
  119. /// 驼峰转下划线命名
  120. /// </summary>
  121. /// <param name="humpString"></param>
  122. /// <returns></returns>
  123. public static string ToUnderline(string humpString)
  124. {
  125. return Regex.Replace(humpString, "([A-Z])", "_$1").ToLower().TrimStart('_');
  126. }
  127. }
  128. }