经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C# » 查看文章
C# string 常用功能的方法扩展
来源:cnblogs  作者:Mr.石  时间:2018/10/12 9:49:46  对本文有异议
  1. 1 #region Usings
  2. 2 using System;
  3. 3 using System.Text;
  4. 4 using System.Data;
  5. 5 using System.Data.SqlClient;
  6. 6 using System.Text.RegularExpressions;
  7. 7 using System.Linq;
  8. 8 using System.Collections.Generic;
  9. 9 using System.ComponentModel;
  10. 10 using DragonUtility.DataTypes.Formatters;
  11. 11 using Microsoft.VisualBasic;
  12. 12 #endregion
  13. 13
  14. 14 namespace DragonUtility.DataTypes.ExtensionMethods
  15. 15 {
  16. 16 /// <summary>
  17. 17 /// String extensions
  18. 18 /// </summary>
  19. 19 public static class StringExtensions
  20. 20 {
  21. 21 #region Functions
  22. 22
  23. 23 #region Encode
  24. 24
  25. 25 /// <summary>
  26. 26 /// 编码转换,把字符串从一种编码转换到另一种编码
  27. 27 /// </summary>
  28. 28 /// <param name="Input">input string</param>
  29. 29 /// <param name="OriginalEncodingUsing">The type of encoding the string is currently using (defaults to ASCII)</param>
  30. 30 /// <param name="EncodingUsing">The type of encoding the string is converted into (defaults to UTF8)</param>
  31. 31 /// <returns>string of the byte array</returns>
  32. 32 public static string Encode(this string Input, Encoding OriginalEncodingUsing = null, Encoding EncodingUsing = null)
  33. 33 {
  34. 34 if (string.IsNullOrEmpty(Input))
  35. 35 return "";
  36. 36 OriginalEncodingUsing = OriginalEncodingUsing.NullCheck(new ASCIIEncoding());
  37. 37 EncodingUsing = EncodingUsing.NullCheck(new UTF8Encoding());
  38. 38 return Encoding.Convert(OriginalEncodingUsing, EncodingUsing, Input.ToByteArray(OriginalEncodingUsing))
  39. 39 .ToEncodedString(EncodingUsing);
  40. 40
  41. 41 }
  42. 42 #endregion
  43. 43
  44. 44 #region FromBase64
  45. 45
  46. 46 /// <summary>
  47. 47 /// Converts base 64 string based on the encoding passed in
  48. 48 /// </summary>
  49. 49 /// <param name="Input">Input string</param>
  50. 50 /// <param name="EncodingUsing">The type of encoding the string is using (defaults to UTF8)</param>
  51. 51 /// <returns>string in the encoding format</returns>
  52. 52 public static string FromBase64(this string Input, Encoding EncodingUsing)
  53. 53 {
  54. 54 if (string.IsNullOrEmpty(Input))
  55. 55 return "";
  56. 56 byte[] TempArray = Convert.FromBase64String(Input);
  57. 57 return EncodingUsing.NullCheck(new UTF8Encoding()).GetString(TempArray);
  58. 58 }
  59. 59
  60. 60 /// <summary>
  61. 61 /// Converts base 64 string to a byte array
  62. 62 /// </summary>
  63. 63 /// <param name="Input">Input string</param>
  64. 64 /// <returns>A byte array equivalent of the base 64 string</returns>
  65. 65 public static byte[] FromBase64(this string Input)
  66. 66 {
  67. 67 return string.IsNullOrEmpty(Input) ? new byte[0] : Convert.FromBase64String(Input);
  68. 68 }
  69. 69
  70. 70 #endregion
  71. 71
  72. 72 #region Left
  73. 73
  74. 74 /// <summary>
  75. 75 /// Gets the first x number of characters from the left hand side
  76. 76 /// </summary>
  77. 77 /// <param name="Input">Input string</param>
  78. 78 /// <param name="Length">x number of characters to return</param>
  79. 79 /// <returns>The resulting string</returns>
  80. 80 public static string Left(this string Input, int Length)
  81. 81 {
  82. 82 return string.IsNullOrEmpty(Input) ? "" : Input.Substring(0, Input.Length > Length ? Length : Input.Length);
  83. 83 }
  84. 84
  85. 85 #endregion
  86. 86
  87. 87 #region Right
  88. 88
  89. 89 /// <summary>
  90. 90 /// Gets the last x number of characters from the right hand side
  91. 91 /// </summary>
  92. 92 /// <param name="Input">Input string</param>
  93. 93 /// <param name="Length">x number of characters to return</param>
  94. 94 /// <returns>The resulting string</returns>
  95. 95 public static string Right(this string Input, int Length)
  96. 96 {
  97. 97 if (string.IsNullOrEmpty(Input))
  98. 98 return "";
  99. 99 Length = Input.Length > Length ? Length : Input.Length;
  100. 100 return Input.Substring(Input.Length - Length, Length);
  101. 101 }
  102. 102
  103. 103 #endregion
  104. 104
  105. 105 #region ToBase64
  106. 106
  107. 107 /// <summary>
  108. 108 /// Converts from the specified encoding to a base 64 string
  109. 109 /// </summary>
  110. 110 /// <param name="Input">Input string</param>
  111. 111 /// <param name="OriginalEncodingUsing">The type of encoding the string is using (defaults to UTF8)</param>
  112. 112 /// <returns>Bas64 string</returns>
  113. 113 public static string ToBase64(this string Input, Encoding OriginalEncodingUsing = null)
  114. 114 {
  115. 115 if (string.IsNullOrEmpty(Input))
  116. 116 return "";
  117. 117 byte[] TempArray = OriginalEncodingUsing.NullCheck(new UTF8Encoding()).GetBytes(Input);
  118. 118 return Convert.ToBase64String(TempArray);
  119. 119 }
  120. 120
  121. 121 #endregion
  122. 122
  123. 123 #region ToByteArray
  124. 124
  125. 125 /// <summary>
  126. 126 /// Converts a string to a byte array
  127. 127 /// </summary>
  128. 128 /// <param name="Input">input string</param>
  129. 129 /// <param name="EncodingUsing">The type of encoding the string is using (defaults to UTF8)</param>
  130. 130 /// <returns>the byte array representing the string</returns>
  131. 131 public static byte[] ToByteArray(this string Input, Encoding EncodingUsing = null)
  132. 132 {
  133. 133 return string.IsNullOrEmpty(Input) ? null : EncodingUsing.NullCheck(new UTF8Encoding()).GetBytes(Input);
  134. 134 }
  135. 135
  136. 136 #endregion
  137. 137
  138. 138 #region ToFirstCharacterUpperCase
  139. 139
  140. 140 /// <summary>
  141. 141 /// Takes the first character of an input string and makes it uppercase
  142. 142 /// </summary>
  143. 143 /// <param name="Input">Input string</param>
  144. 144 /// <returns>String with the first character capitalized</returns>
  145. 145 public static string ToFirstCharacterUpperCase(this string Input)
  146. 146 {
  147. 147 if (string.IsNullOrEmpty(Input))
  148. 148 return "";
  149. 149 char[] InputChars = Input.ToCharArray();
  150. 150 for (int x = 0; x < InputChars.Length; ++x)
  151. 151 {
  152. 152 if (InputChars[x] != ' ' && InputChars[x] != '\t')
  153. 153 {
  154. 154 InputChars[x] = char.ToUpper(InputChars[x]);
  155. 155 break;
  156. 156 }
  157. 157 }
  158. 158 return new string(InputChars);
  159. 159 }
  160. 160
  161. 161 #endregion
  162. 162
  163. 163 #region ToSentenceCapitalize
  164. 164
  165. 165 /// <summary>
  166. 166 /// Capitalizes each sentence within the string
  167. 167 /// </summary>
  168. 168 /// <param name="Input">Input string</param>
  169. 169 /// <returns>String with each sentence capitalized</returns>
  170. 170 public static string ToSentenceCapitalize(this string Input)
  171. 171 {
  172. 172 if (string.IsNullOrEmpty(Input))
  173. 173 return "";
  174. 174 string[] Seperator = { ".", "?", "!" };
  175. 175 string[] InputStrings = Input.Split(Seperator, StringSplitOptions.None);
  176. 176 for (int x = 0; x < InputStrings.Length; ++x)
  177. 177 {
  178. 178 if (!string.IsNullOrEmpty(InputStrings[x]))
  179. 179 {
  180. 180 Regex TempRegex = new Regex(InputStrings[x]);
  181. 181 InputStrings[x] = InputStrings[x].ToFirstCharacterUpperCase();
  182. 182 Input = TempRegex.Replace(Input, InputStrings[x]);
  183. 183 }
  184. 184 }
  185. 185 return Input;
  186. 186 }
  187. 187
  188. 188 #endregion
  189. 189
  190. 190 #region ToTitleCase
  191. 191
  192. 192 /// <summary>
  193. 193 /// Capitalizes the first character of each word
  194. 194 /// </summary>
  195. 195 /// <param name="Input">Input string</param>
  196. 196 /// <returns>String with each word capitalized</returns>
  197. 197 public static string ToTitleCase(this string Input)
  198. 198 {
  199. 199 if (string.IsNullOrEmpty(Input))
  200. 200 return "";
  201. 201 string[] Seperator = { " ", ".", "\t", System.Environment.NewLine, "!", "?" };
  202. 202 string[] InputStrings = Input.Split(Seperator, StringSplitOptions.None);
  203. 203 for (int x = 0; x < InputStrings.Length; ++x)
  204. 204 {
  205. 205 if (!string.IsNullOrEmpty(InputStrings[x])
  206. 206 && InputStrings[x].Length > 3)
  207. 207 {
  208. 208 Regex TempRegex = new Regex(InputStrings[x]);
  209. 209 InputStrings[x] = InputStrings[x].ToFirstCharacterUpperCase();
  210. 210 Input = TempRegex.Replace(Input, InputStrings[x]);
  211. 211 }
  212. 212 }
  213. 213 return Input;
  214. 214 }
  215. 215
  216. 216 #endregion
  217. 217
  218. 218 #region NumberTimesOccurs
  219. 219
  220. 220 /// <summary>
  221. 221 /// returns the number of times a string occurs within the text
  222. 222 /// </summary>
  223. 223 /// <param name="Input">input text</param>
  224. 224 /// <param name="Match">The string to match (can be regex)</param>
  225. 225 /// <returns>The number of times the string occurs</returns>
  226. 226 public static int NumberTimesOccurs(this string Input, string Match)
  227. 227 {
  228. 228 return string.IsNullOrEmpty(Input) ? 0 : new Regex(Match).Matches(Input).Count;
  229. 229 }
  230. 230
  231. 231 #endregion
  232. 232
  233. 233 #region Reverse
  234. 234
  235. 235 /// <summary>
  236. 236 /// Reverses a string
  237. 237 /// </summary>
  238. 238 /// <param name="Input">Input string</param>
  239. 239 /// <returns>The reverse of the input string</returns>
  240. 240 public static string Reverse(this string Input)
  241. 241 {
  242. 242 return new string(Input.Reverse<char>().ToArray());
  243. 243 }
  244. 244
  245. 245 #endregion
  246. 246
  247. 247 #region FilterOutText
  248. 248
  249. 249 /// <summary>
  250. 250 /// Removes the filter text from the input.
  251. 251 /// </summary>
  252. 252 /// <param name="Input">Input text</param>
  253. 253 /// <param name="Filter">Regex expression of text to filter out</param>
  254. 254 /// <returns>The input text minus the filter text.</returns>
  255. 255 public static string FilterOutText(this string Input, string Filter)
  256. 256 {
  257. 257 if (string.IsNullOrEmpty(Input))
  258. 258 return "";
  259. 259 return string.IsNullOrEmpty(Filter) ? Input : new Regex(Filter).Replace(Input, "");
  260. 260 }
  261. 261
  262. 262 #endregion
  263. 263
  264. 264 #region KeepFilterText
  265. 265
  266. 266 /// <summary>
  267. 267 /// Removes everything that is not in the filter text from the input.
  268. 268 /// </summary>
  269. 269 /// <param name="Input">Input text</param>
  270. 270 /// <param name="Filter">Regex expression of text to keep</param>
  271. 271 /// <returns>The input text minus everything not in the filter text.</returns>
  272. 272 public static string KeepFilterText(this string Input, string Filter)
  273. 273 {
  274. 274 if (string.IsNullOrEmpty(Input) || string.IsNullOrEmpty(Filter))
  275. 275 return "";
  276. 276 Regex TempRegex = new Regex(Filter);
  277. 277 MatchCollection Collection = TempRegex.Matches(Input);
  278. 278 StringBuilder Builder = new StringBuilder();
  279. 279 foreach (Match Match in Collection)
  280. 280 Builder.Append(Match.Value);
  281. 281 return Builder.ToString();
  282. 282 }
  283. 283
  284. 284 #endregion
  285. 285
  286. 286 #region AlphaNumericOnly
  287. 287
  288. 288 /// <summary>
  289. 289 /// Keeps only alphanumeric characters
  290. 290 /// </summary>
  291. 291 /// <param name="Input">Input string</param>
  292. 292 /// <returns>the string only containing alphanumeric characters</returns>
  293. 293 public static string AlphaNumericOnly(this string Input)
  294. 294 {
  295. 295 return Input.KeepFilterText("[a-zA-Z0-9]");
  296. 296 }
  297. 297
  298. 298 #endregion
  299. 299
  300. 300 #region AlphaCharactersOnly
  301. 301
  302. 302 /// <summary>
  303. 303 /// Keeps only alpha characters
  304. 304 /// </summary>
  305. 305 /// <param name="Input">Input string</param>
  306. 306 /// <returns>the string only containing alpha characters</returns>
  307. 307 public static string AlphaCharactersOnly(this string Input)
  308. 308 {
  309. 309 return Input.KeepFilterText("[a-zA-Z]");
  310. 310 }
  311. 311
  312. 312 #endregion
  313. 313
  314. 314 #region NumericOnly
  315. 315
  316. 316 /// <summary>
  317. 317 /// Keeps only numeric characters
  318. 318 /// </summary>
  319. 319 /// <param name="Input">Input string</param>
  320. 320 /// <param name="KeepNumericPunctuation">Determines if decimal places should be kept</param>
  321. 321 /// <returns>the string only containing numeric characters</returns>
  322. 322 public static string NumericOnly(this string Input, bool KeepNumericPunctuation = true)
  323. 323 {
  324. 324 return KeepNumericPunctuation ? Input.KeepFilterText(@"[0-9\.]") : Input.KeepFilterText("[0-9]");
  325. 325 }
  326. 326
  327. 327 #endregion
  328. 328
  329. 329 #region IsUnicode
  330. 330
  331. 331 /// <summary>
  332. 332 /// Determines if a string is unicode
  333. 333 /// </summary>
  334. 334 /// <param name="Input">Input string</param>
  335. 335 /// <returns>True if it's unicode, false otherwise</returns>
  336. 336 public static bool IsUnicode(this string Input)
  337. 337 {
  338. 338 return string.IsNullOrEmpty(Input) ? true : Regex.Replace(Input, @"[^\u0000-\u007F]", "") != Input;
  339. 339 }
  340. 340
  341. 341 #endregion
  342. 342
  343. 343 #region FormatString
  344. 344
  345. 345 /// <summary>
  346. 346 /// Formats a string based on a format string passed in:
  347. 347 /// # = digits
  348. 348 /// @ = alpha characters
  349. 349 /// \ = escape char
  350. 350 /// </summary>
  351. 351 /// <param name="Input">Input string</param>
  352. 352 /// <param name="Format">Format of the output string</param>
  353. 353 /// <returns>The formatted string</returns>
  354. 354 public static string FormatString(this string Input, string Format)
  355. 355 {
  356. 356 return new GenericStringFormatter().Format(Input, Format);
  357. 357 }
  358. 358
  359. 359 #endregion
  360. 360
  361. 361 #region RegexFormat
  362. 362
  363. 363 /// <summary>
  364. 364 /// Uses a regex to format the input string
  365. 365 /// </summary>
  366. 366 /// <param name="Input">Input string</param>
  367. 367 /// <param name="Format">Regex string used to</param>
  368. 368 /// <param name="OutputFormat">Output format</param>
  369. 369 /// <param name="Options">Regex options</param>
  370. 370 /// <returns>The input string formatted by using the regex string</returns>
  371. 371 public static string RegexFormat(this string Input, string Format, string OutputFormat, RegexOptions Options = RegexOptions.None)
  372. 372 {
  373. 373 Input.ThrowIfNullOrEmpty("Input");
  374. 374 return Regex.Replace(Input, Format, OutputFormat, Options);
  375. 375 }
  376. 376
  377. 377 #endregion
  378. 378
  379. 379 #region 转换
  380. 380 /// <summary>
  381. 381 /// 全角转半角
  382. 382 /// </summary>
  383. 383 /// <param name="input">要转换的字符串</param>
  384. 384 /// <returns>转换完的字符串</returns>
  385. 385 public static string Narrow(this string input)
  386. 386 {
  387. 387 return Strings.StrConv(input, VbStrConv.Narrow, 0);
  388. 388 }
  389. 389 /// <summary>
  390. 390 /// 半角转全角
  391. 391 /// </summary>
  392. 392 /// <param name="input">要转换的字符串</param>
  393. 393 /// <returns>转换完的字符串</returns>
  394. 394 public static string Wide(this string input)
  395. 395 {
  396. 396 return Strings.StrConv(input, VbStrConv.Wide, 0);
  397. 397 }
  398. 398 /// <summary>
  399. 399 /// 简体转繁体
  400. 400 /// </summary>
  401. 401 /// <param name="input"></param>
  402. 402 /// <returns></returns>
  403. 403 public static string TraditionalChinese(this string input)
  404. 404 {
  405. 405 return Strings.StrConv(input, VbStrConv.TraditionalChinese, 0);
  406. 406 }
  407. 407 /// <summary>
  408. 408 /// 繁体转简体
  409. 409 /// </summary>
  410. 410 /// <param name="input"></param>
  411. 411 /// <returns></returns>
  412. 412 public static string SimplifiedChinese(this string input)
  413. 413 {
  414. 414 return Strings.StrConv(input, VbStrConv.SimplifiedChinese, 0);
  415. 415 }
  416. 416 /// <summary>
  417. 417 /// 将每个单词首字母大写
  418. 418 /// </summary>
  419. 419 /// <param name="input"></param>
  420. 420 /// <returns></returns>
  421. 421 public static string ProperCase(this string input)
  422. 422 {
  423. 423 return Strings.StrConv(input, VbStrConv.ProperCase, 0);
  424. 424 }
  425. 425 #endregion
  426. 426
  427. 427 #endregion
  428. 428 }
  429. 429 }

 

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号