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 6.2 kB

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