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

77 lines
2.4 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace Apewer.Internals
  5. {
  6. internal class TextHelper
  7. {
  8. /// <summary>获取文本的长度。</summary>
  9. public static int Len(string argOrigin)
  10. {
  11. return string.IsNullOrEmpty(argOrigin) ? 0 : argOrigin.Length;
  12. }
  13. /// <summary>将文本转换为小写。</summary>
  14. public static string LCase(string argOrigin)
  15. {
  16. return string.IsNullOrEmpty(argOrigin) ? "" : argOrigin.ToLower();
  17. }
  18. /// <summary>将文本转换为大写。</summary>
  19. public static string UCase(string argOrigin)
  20. {
  21. return string.IsNullOrEmpty(argOrigin) ? "" : argOrigin.ToLower();
  22. }
  23. /// <summary>获取文本中间的部分,起始位置从 0 开始。</summary>
  24. public static string Mid(string argParent, int argStart, int argLength)
  25. {
  26. return Middle(argParent, argStart, argLength);
  27. }
  28. /// <summary>获取文本的位置。</summary>
  29. public static int InStr(string argParent, string argSub)
  30. {
  31. if (string.IsNullOrEmpty(argParent)) return -1;
  32. if (string.IsNullOrEmpty(argSub)) return -1;
  33. return argParent.IndexOf(argSub);
  34. }
  35. /// <summary>获取文本中间的部分,起始位置从 0 开始。</summary>
  36. public static string Middle(string argParent, int argStart, int argLength)
  37. {
  38. if (string.IsNullOrEmpty(argParent)) return "";
  39. var vstart = argStart;
  40. if (vstart < 0) vstart = 0;
  41. if (vstart >= argParent.Length) return "";
  42. var vlength = argLength;
  43. if (vlength <= 0) return "";
  44. if ((vlength + vstart) > argParent.Length)
  45. {
  46. vlength = argParent.Length - vstart;
  47. }
  48. return argParent.Substring(vstart, vlength);
  49. }
  50. /// <summary>获取文本左边的部分。</summary>
  51. public static string Left(string argParent, int argLength)
  52. {
  53. return Middle(argParent, 0, argLength);
  54. }
  55. /// <summary>获取文本右边的部分。</summary>
  56. public static string Right(string argParent, int argLength)
  57. {
  58. if (string.IsNullOrEmpty(argParent)) return "";
  59. return Middle(argParent, argParent.Length - argLength, argLength);
  60. }
  61. }
  62. }