StringExt.cs 7.0 KB

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