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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. #if EXPOSE_EVERYTHING || EXPOSE_STRINGEX
  8. public
  9. #endif
  10. static partial class StringEx
  11. {
  12. #pragma warning disable 1591
  13. public static StringComparison GlobalDefaultComparison { get; set; } = StringComparison.Ordinal;
  14. [ThreadStatic]
  15. private static StringComparison? _DefaultComparison;
  16. public static StringComparison DefaultComparison
  17. {
  18. get { return _DefaultComparison ?? GlobalDefaultComparison; }
  19. set { _DefaultComparison = value; }
  20. }
  21. #region basic String methods
  22. public static bool IsNullOrEmpty(this string value)
  23. => string.IsNullOrEmpty(value);
  24. public static bool IsNullOrWhiteSpace(this string value)
  25. => string.IsNullOrWhiteSpace(value);
  26. public static bool IsWhiteSpace(this string value)
  27. {
  28. foreach (var c in value)
  29. {
  30. if (char.IsWhiteSpace(c)) continue;
  31. return false;
  32. }
  33. return true;
  34. }
  35. #if !PCL
  36. public static string IsInterned(this string value)
  37. {
  38. if (value == null)
  39. throw new ArgumentNullException(nameof(value));
  40. return string.IsInterned(value);
  41. }
  42. public static string Intern(this string value)
  43. {
  44. if (value == null)
  45. throw new ArgumentNullException(nameof(value));
  46. return string.Intern(value);
  47. }
  48. #endif
  49. #if UNSAFE
  50. public static unsafe string ToLowerForASCII(this string value)
  51. {
  52. if (value.IsNullOrWhiteSpace())
  53. return value;
  54. value = string.Copy(value);
  55. fixed (char* low = value)
  56. {
  57. var end = low + value.Length;
  58. for (var p = low; p < end; p++)
  59. {
  60. var c = *p;
  61. if (c < 'A' || c > 'Z')
  62. continue;
  63. *p = (char)(c + 0x20);
  64. }
  65. }
  66. return value;
  67. }
  68. public static unsafe string ToUpperForASCII(this string value)
  69. {
  70. if (value.IsNullOrWhiteSpace())
  71. return value;
  72. value = string.Copy(value);
  73. fixed (char* low = value)
  74. {
  75. var end = low + value.Length;
  76. for (var p = low; p < end; p++)
  77. {
  78. var c = *p;
  79. if (c < 'a' || c > 'z')
  80. continue;
  81. *p = (char)(c - 0x20);
  82. }
  83. }
  84. return value;
  85. }
  86. #else
  87. public static string ToLowerForASCII(this string value)
  88. {
  89. if (value.IsNullOrWhiteSpace())
  90. return value;
  91. var sb = new StringBuilder(value.Length);
  92. foreach (var c in value)
  93. {
  94. if (c < 'A' || c > 'Z')
  95. sb.Append(c);
  96. else
  97. sb.Append((char)(c + 0x20));
  98. }
  99. return sb.ToString();
  100. }
  101. public static string ToUpperForASCII(this string value)
  102. {
  103. if (value.IsNullOrWhiteSpace())
  104. return value;
  105. var sb = new StringBuilder(value.Length);
  106. foreach (var c in value)
  107. {
  108. if (c < 'a' || c > 'z')
  109. sb.Append(c);
  110. else
  111. sb.Append((char)(c - 0x20));
  112. }
  113. return sb.ToString();
  114. }
  115. #endif
  116. #endregion
  117. #region comparing
  118. #region Is
  119. public static bool Is(this string a, string b)
  120. => string.Equals(a, b, DefaultComparison);
  121. public static bool Is(this string a, string b, StringComparison comparisonType)
  122. => string.Equals(a, b, comparisonType);
  123. #endregion
  124. #region BeginWith
  125. public static bool BeginWith(this string s, char c)
  126. {
  127. if (s.IsNullOrEmpty()) return false;
  128. return s[0] == c;
  129. }
  130. public static bool BeginWithAny(this string s, IEnumerable<char> chars)
  131. {
  132. if (s.IsNullOrEmpty()) return false;
  133. return chars.Contains(s[0]);
  134. }
  135. public static bool BeginWithAny(this string s, params char[] chars)
  136. => s.BeginWithAny(chars.AsEnumerable());
  137. public static bool BeginWith(this string a, string b)
  138. {
  139. if (a == null || b == null) return false;
  140. return a.StartsWith(b, DefaultComparison);
  141. }
  142. public static bool BeginWith(this string a, string b, StringComparison comparisonType)
  143. {
  144. if (a == null || b == null) return false;
  145. return a.StartsWith(b, comparisonType);
  146. }
  147. #if !PCL
  148. public static bool BeginWith(this string a, string b, bool ignoreCase, CultureInfo culture)
  149. {
  150. if (a == null || b == null) return false;
  151. return a.StartsWith(b, ignoreCase, culture);
  152. }
  153. #endif
  154. #endregion
  155. #region FinishWith
  156. public static bool FinishWith(this string s, char c)
  157. {
  158. if (s.IsNullOrEmpty()) return false;
  159. return s.Last() == c;
  160. }
  161. public static bool FinishWithAny(this string s, IEnumerable<char> chars)
  162. {
  163. if (s.IsNullOrEmpty()) return false;
  164. return chars.Contains(s.Last());
  165. }
  166. public static bool FinishWithAny(this string s, params char[] chars)
  167. => s.FinishWithAny(chars.AsEnumerable());
  168. public static bool FinishWith(this string a, string b)
  169. {
  170. if (a == null || b == null) return false;
  171. return a.EndsWith(b, DefaultComparison);
  172. }
  173. public static bool FinishWith(this string a, string b, StringComparison comparisonType)
  174. {
  175. if (a == null || b == null) return false;
  176. return a.EndsWith(b, comparisonType);
  177. }
  178. #if !PCL
  179. public static bool FinishWith(this string a, string b, bool ignoreCase, CultureInfo culture)
  180. {
  181. if (a == null || b == null) return false;
  182. return a.EndsWith(b, ignoreCase, culture);
  183. }
  184. #endif
  185. #endregion
  186. #endregion
  187. #region ToLines
  188. public static IEnumerable<string> ToLines(this TextReader reader)
  189. {
  190. string line;
  191. while ((line = reader.ReadLine()) != null)
  192. yield return line;
  193. }
  194. public static IEnumerable<string> NonEmptyLines(this TextReader reader)
  195. {
  196. string line;
  197. while ((line = reader.ReadLine()) != null)
  198. {
  199. if (line == "") continue;
  200. yield return line;
  201. }
  202. }
  203. public static IEnumerable<string> NonWhiteSpaceLines(this TextReader reader)
  204. {
  205. string line;
  206. while ((line = reader.ReadLine()) != null)
  207. {
  208. if (line.IsWhiteSpace()) continue;
  209. yield return line;
  210. }
  211. }
  212. #endregion
  213. #region others
  214. private static readonly char[][] Quotes = new[]
  215. {
  216. "\"\"",
  217. "''",
  218. "“”",
  219. "‘’",
  220. "『』",
  221. "「」",
  222. "〖〗",
  223. "【】",
  224. }.Select(s => s.ToCharArray()).ToArray();
  225. public static string Enquote(this string value)
  226. {
  227. if (value == null)
  228. return "(null)";
  229. foreach (var pair in Quotes)
  230. {
  231. if (value.IndexOfAny(pair) < 0)
  232. return pair[0] + value + pair[1];
  233. }
  234. return '"' + value.Replace("\\", @"\\").Replace("\"", @"\""") + '"';
  235. }
  236. public static string Replace(this string value, string find, string rep, StringComparison comparsionType)
  237. {
  238. if (find.IsNullOrEmpty())
  239. throw new ArgumentException(null, nameof(find));
  240. if (rep == null)
  241. rep = "";
  242. if (value.IsNullOrEmpty())
  243. return value;
  244. var sb = new StringBuilder(value.Length);
  245. var last = 0;
  246. var len = find.Length;
  247. var idx = value.IndexOf(find, DefaultComparison);
  248. while (idx != -1)
  249. {
  250. sb.Append(value.Substring(last, idx - last));
  251. sb.Append(rep);
  252. idx += len;
  253. last = idx;
  254. idx = value.IndexOf(find, idx, comparsionType);
  255. }
  256. sb.Append(value.Substring(last));
  257. return sb.ToString();
  258. }
  259. public static string ReplaceEx(this string value, string find, string rep)
  260. => value.Replace(find, rep, DefaultComparison);
  261. #endregion
  262. }