You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

StringEx.cs 5.2 kB

9 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Text;
  6. static partial class StringEx
  7. {
  8. #pragma warning disable 1591
  9. public static StringComparison GlobalDefaultComparison { get; set; } = StringComparison.Ordinal;
  10. [ThreadStatic]
  11. private static StringComparison? _DefaultComparison;
  12. public static StringComparison DefaultComparison
  13. {
  14. get { return _DefaultComparison ?? GlobalDefaultComparison; }
  15. set { _DefaultComparison = value; }
  16. }
  17. #region basic String methods
  18. public static bool IsNullOrEmpty(this string value)
  19. => string.IsNullOrEmpty(value);
  20. public static bool IsNullOrWhiteSpace(this string value)
  21. => string.IsNullOrWhiteSpace(value);
  22. #if !PCL
  23. public static string IsInterned(this string value)
  24. {
  25. if (value == null)
  26. throw new ArgumentNullException(nameof(value));
  27. return string.IsInterned(value);
  28. }
  29. public static string Intern(this string value)
  30. {
  31. if (value == null)
  32. throw new ArgumentNullException(nameof(value));
  33. return string.Intern(value);
  34. }
  35. #endif
  36. #endregion
  37. #region comparing
  38. #region Is
  39. public static bool Is(this string a, string b)
  40. => string.Equals(a, b, DefaultComparison);
  41. public static bool Is(this string a, string b, StringComparison comparisonType)
  42. => string.Equals(a, b, comparisonType);
  43. #endregion
  44. #region BeginWith
  45. public static bool BeginWith(this string s, char c)
  46. {
  47. if (s.IsNullOrEmpty()) return false;
  48. return s[0] == c;
  49. }
  50. public static bool BeginWithAny(this string s, IEnumerable<char> chars)
  51. {
  52. if (s.IsNullOrEmpty()) return false;
  53. return chars.Contains(s[0]);
  54. }
  55. public static bool BeginWithAny(this string s, params char[] chars)
  56. => s.BeginWithAny(chars.AsEnumerable());
  57. public static bool BeginWith(this string a, string b)
  58. {
  59. if (a == null || b == null) return false;
  60. return a.StartsWith(b, DefaultComparison);
  61. }
  62. public static bool BeginWith(this string a, string b, StringComparison comparisonType)
  63. {
  64. if (a == null || b == null) return false;
  65. return a.StartsWith(b, comparisonType);
  66. }
  67. #if !PCL
  68. public static bool BeginWith(this string a, string b, bool ignoreCase, CultureInfo culture)
  69. {
  70. if (a == null || b == null) return false;
  71. return a.StartsWith(b, ignoreCase, culture);
  72. }
  73. #endif
  74. #endregion
  75. #region FinishWith
  76. public static bool FinishWith(this string s, char c)
  77. {
  78. if (s.IsNullOrEmpty()) return false;
  79. return s.Last() == c;
  80. }
  81. public static bool FinishWithAny(this string s, IEnumerable<char> chars)
  82. {
  83. if (s.IsNullOrEmpty()) return false;
  84. return chars.Contains(s.Last());
  85. }
  86. public static bool FinishWithAny(this string s, params char[] chars)
  87. => s.FinishWithAny(chars.AsEnumerable());
  88. public static bool FinishWith(this string a, string b)
  89. {
  90. if (a == null || b == null) return false;
  91. return a.EndsWith(b, DefaultComparison);
  92. }
  93. public static bool FinishWith(this string a, string b, StringComparison comparisonType)
  94. {
  95. if (a == null || b == null) return false;
  96. return a.EndsWith(b, comparisonType);
  97. }
  98. #if !PCL
  99. public static bool FinishWith(this string a, string b, bool ignoreCase, CultureInfo culture)
  100. {
  101. if (a == null || b == null) return false;
  102. return a.EndsWith(b, ignoreCase, culture);
  103. }
  104. #endif
  105. #endregion
  106. #endregion
  107. #region others
  108. private static readonly char[][] Quotes = new[]
  109. {
  110. "\"\"",
  111. "''",
  112. "“”",
  113. "‘’",
  114. "『』",
  115. "「」",
  116. "〖〗",
  117. "【】",
  118. }.Select(s => s.ToCharArray()).ToArray();
  119. public static string Enquote(this string value)
  120. {
  121. if (value == null)
  122. return "(null)";
  123. foreach (var pair in Quotes)
  124. {
  125. if (value.IndexOfAny(pair) < 0)
  126. return pair[0] + value + pair[1];
  127. }
  128. return '"' + value.Replace("\\", @"\\").Replace("\"", @"\""") + '"';
  129. }
  130. public static string Replace(this string value, string find, string rep, StringComparison comparsionType)
  131. {
  132. if (find.IsNullOrEmpty())
  133. throw new ArgumentException(null, nameof(find));
  134. if (rep == null)
  135. rep = "";
  136. if (value.IsNullOrEmpty())
  137. return value;
  138. var sb = new StringBuilder(value.Length);
  139. var last = 0;
  140. var len = find.Length;
  141. var idx = value.IndexOf(find, DefaultComparison);
  142. while (idx != -1)
  143. {
  144. sb.Append(value.Substring(last, idx - last));
  145. sb.Append(rep);
  146. idx += len;
  147. last = idx;
  148. idx = value.IndexOf(find, idx, comparsionType);
  149. }
  150. sb.Append(value.Substring(last));
  151. return sb.ToString();
  152. }
  153. public static string ReplaceEx(this string value, string find, string rep)
  154. => value.Replace(find, rep, DefaultComparison);
  155. #endregion
  156. }