StringExt.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. namespace Quadarax.Foundation.Core.Value.Extensions
  6. {
  7. public static class StringExt
  8. {
  9. public static string Align(this string value, int size)
  10. {
  11. if (string.IsNullOrEmpty(value)) return new string(' ', size);
  12. if (value.Length < size)
  13. return value + new string(' ', size - value.Length);
  14. return Truncate(value, size);
  15. }
  16. public static string Truncate(this string value, int maxLength)
  17. {
  18. if (string.IsNullOrEmpty(value)) return value;
  19. return value.Length <= maxLength ? value : value.Substring(0, maxLength);
  20. }
  21. public static int SeparateValueCount(this string value, string separator)
  22. {
  23. var nSepLen = separator.Length;
  24. var nStrLen = value.Length;
  25. if (nStrLen == 0 || nSepLen == 0)
  26. return nSepLen == 0 && nStrLen > 0 ? 1 : 0;
  27. var nCnt = 0;
  28. var nPos = value.IndexOf(separator, StringComparison.Ordinal);
  29. while (nPos >= 0)
  30. {
  31. nCnt++;
  32. nPos = value.IndexOf(separator, nPos + nSepLen, StringComparison.Ordinal);
  33. }
  34. return nCnt + 1;
  35. }
  36. public static string SeparateValue(this string value, string separator, int zeroBasedIndex)
  37. {
  38. var nSepLen = separator.Length;
  39. var nStrLen = value.Length;
  40. if (nStrLen == 0 || nSepLen == 0 || nSepLen > nStrLen)
  41. throw new IndexOutOfRangeException("SeparateValue method has index out of range.");
  42. var nLastPos = 0;
  43. var nPos = value.IndexOf(separator, StringComparison.Ordinal);
  44. while (nPos >= 0)
  45. {
  46. if (zeroBasedIndex == 0)
  47. break;
  48. nLastPos = nPos + nSepLen;
  49. nPos = value.IndexOf(separator, nPos + nSepLen, StringComparison.Ordinal);
  50. zeroBasedIndex--;
  51. }
  52. if (zeroBasedIndex > 0)
  53. throw new IndexOutOfRangeException("SeparateValue method has index out of range.");
  54. return value.Substring(nLastPos, nPos == -1 ? nStrLen - nLastPos : nPos - nLastPos);
  55. }
  56. public static string SeparateValueLast(this string value, string separator)
  57. {
  58. var nSepLen = separator.Length;
  59. var nStrLen = value.Length;
  60. if (nStrLen == 0 || nSepLen == 0 || nSepLen > nStrLen)
  61. throw new IndexOutOfRangeException("SeparateValue method has index out of range.");
  62. return value.SeparateValue(separator, value.SeparateValueCount(separator) - 1);
  63. }
  64. public static string RemoveLast(this string value)
  65. {
  66. return value.Length == 0 ? string.Empty : value.Substring(0, value.Length - 1);
  67. }
  68. public static string RemoveFirst(this string value)
  69. {
  70. return value.Length == 0 ? string.Empty : value.Substring(1, value.Length - 1);
  71. }
  72. public static int MaxLineChars(this string value)
  73. {
  74. var aParts = value.Split("/n");
  75. return aParts.Length > 0 ? aParts.Max(x => x.Length) : value.Length;
  76. }
  77. public static string[] Split(this string value, string separator)
  78. {
  79. return value.Split(new[] { separator }, StringSplitOptions.RemoveEmptyEntries);
  80. }
  81. public static Tuple<string, string>[] Extract(this string value, params string[] hashes) //hashtype, tashname
  82. {
  83. var oResult = new List<Tuple<string, string>>();
  84. if (hashes.Length == 0)
  85. return new Tuple<string, string>[0];
  86. var nLenIndex = value.Length + 1;
  87. foreach (var sHash in hashes)
  88. {
  89. var bBeginBlock = true;
  90. var nPos = value.IndexOf(sHash, StringComparison.InvariantCulture);
  91. var nLenHash = sHash.Length;
  92. if (nPos < 0)
  93. continue;
  94. while (nPos >= 0)
  95. {
  96. if (nPos + nLenHash > nLenIndex)
  97. break;
  98. var nEndPos = value.IndexOf(sHash, nPos + nLenHash, StringComparison.InvariantCulture);
  99. if (nEndPos < 0)
  100. break;
  101. if (bBeginBlock)
  102. {
  103. var oEntry = new Tuple<string, string>(sHash, value.Substring(nPos + nLenHash, nEndPos - nPos - nLenHash));
  104. if (!oResult.Contains(oEntry))
  105. oResult.Add(oEntry);
  106. }
  107. nPos = nEndPos;
  108. bBeginBlock = !bBeginBlock;
  109. }
  110. }
  111. return oResult.ToArray();
  112. }
  113. public static string EnsurePathBackslash(this string value)
  114. {
  115. if (string.IsNullOrEmpty(value))
  116. return value;
  117. value = value.Trim();
  118. if (value.EndsWith(Path.DirectorySeparatorChar))
  119. return value;
  120. return value + Path.DirectorySeparatorChar;
  121. }
  122. public static string EnsurePathNonBackslash(this string value)
  123. {
  124. if (string.IsNullOrEmpty(value))
  125. return value;
  126. value = value.Trim();
  127. if (!value.EndsWith(Path.DirectorySeparatorChar))
  128. return value;
  129. return value.RemoveLast();
  130. }
  131. public static string RemoveWhitespace(this string input)
  132. {
  133. return new string(input.ToCharArray()
  134. .Where(c => !Char.IsWhiteSpace(c))
  135. .ToArray());
  136. }
  137. }
  138. }