StringExtension.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. using System;
  2. using System.Text.RegularExpressions;
  3. namespace Extensions
  4. {
  5. public static class stringExtension
  6. {
  7. public static T ToJson<T>(this string obj)
  8. {
  9. return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(obj);
  10. }
  11. public static bool IsNullOrEmpty(this string @this)
  12. {
  13. return string.IsNullOrEmpty(@this);
  14. }
  15. public static Nullable<T> ToNullable<T>(this string @this) where T:struct
  16. {
  17. if (string.IsNullOrWhiteSpace(@this)) return null;
  18. var val=Convert.ChangeType(@this, typeof(T));
  19. return (T)val;
  20. }
  21. public static bool IsNotNullOrEmpty(this string @this)
  22. {
  23. return !string.IsNullOrEmpty(@this);
  24. }
  25. public static bool IsNullOrWhitespace(this string @this)
  26. {
  27. return string.IsNullOrWhiteSpace(@this);
  28. }
  29. public static bool IsNotNullOrWhitespace(this string @this)
  30. {
  31. return !string.IsNullOrWhiteSpace(@this);
  32. }
  33. public static string IfNullOrEmpty(this string @this, string defaultVal)
  34. {
  35. return string.IsNullOrEmpty(@this) ? defaultVal : @this;
  36. }
  37. public static string IfNullOrWhitespace(this string @this, string defaultVal)
  38. {
  39. return string.IsNullOrWhiteSpace(@this) ? defaultVal : @this;
  40. }
  41. public static string ClearWhitespace(this string @this)
  42. {
  43. if (string.IsNullOrWhiteSpace(@this)) return string.Empty;
  44. return @this.Replace(" ", string.Empty);
  45. }
  46. public static string Left(this string @this, int length)
  47. {
  48. if (string.IsNullOrEmpty(@this)) return string.Empty;
  49. return @this.Length < length ? @this : @this.Substring(0, length);
  50. }
  51. public static string Rigth(this string @this, int length)
  52. {
  53. if (string.IsNullOrEmpty(@this)) return string.Empty;
  54. return @this.Length < length ? @this : @this.Substring(@this.Length - length);
  55. }
  56. public static string TrimToMaxLength(this string @this, int maxLength, string suffix = "")
  57. {
  58. return ((@this == null || @this.Length <= maxLength) ? @this : string.Concat(@this.Substring(0, maxLength), suffix));
  59. }
  60. public static T To<T>(this string @this)
  61. {
  62. return To(@this, default(T));
  63. }
  64. public static T To<T>(this string @this, T defaultValue)
  65. {
  66. if (string.IsNullOrWhiteSpace(@this))
  67. return defaultValue;
  68. T retVal = defaultValue;
  69. try
  70. {
  71. //获取要转换的目标类型
  72. Type targetType = typeof(T);
  73. //对 Guid 类型的值进行单独处理
  74. if (targetType == typeof(Guid))
  75. retVal = (T)((object)(new Guid(@this)));
  76. //对 Enum 类型的值进行单独处理
  77. else if (targetType.BaseType == typeof(Enum))
  78. retVal = (T)Enum.Parse(targetType, @this);
  79. else
  80. retVal = (T)Convert.ChangeType(@this, targetType);
  81. }
  82. catch { }
  83. return retVal;
  84. }
  85. public static string GetBefore(this string value, string x)
  86. {
  87. if (value == null || x == null)
  88. return string.Empty;
  89. int xPos = value.IndexOf(x);
  90. return xPos == -1 ? string.Empty : value.Substring(0, xPos);
  91. }
  92. public static string GetAfter(this string value, string x)
  93. {
  94. if (value == null || x == null)
  95. return string.Empty;
  96. int xPos = value.LastIndexOf(x);
  97. if (xPos == -1)
  98. return string.Empty;
  99. int startIndex = xPos + x.Length;
  100. return startIndex >= value.Length ? string.Empty : value.Substring(startIndex);
  101. }
  102. public static string GetBetween(this string value, string x, string y)
  103. {
  104. if (value == null || x == null || y == null)
  105. return string.Empty;
  106. int xPos = value.IndexOf(x);
  107. int yPos = value.LastIndexOf(y);
  108. if (xPos == -1 || yPos == -1)
  109. return string.Empty;
  110. int startIndex = xPos + x.Length;
  111. return startIndex >= yPos ? string.Empty : value.Substring(startIndex, yPos - startIndex);
  112. }
  113. public static string FormatWith(this string @this, Object arg0)
  114. {
  115. return string.Format(@this, arg0);
  116. }
  117. public static string FormatWith(this string @this, Object arg0, Object arg1)
  118. {
  119. return string.Format(@this, arg0, arg1);
  120. }
  121. public static string FormatWith(this string @this, Object arg0, Object arg1, Object arg2)
  122. {
  123. return string.Format(@this, arg0, arg1, arg2);
  124. }
  125. public static string FormatWith(this string @this, params object[] values)
  126. {
  127. return string.Format(@this, values);
  128. }
  129. public static string[] Split(this string @this, string separator, StringSplitOptions option = StringSplitOptions.None)
  130. {
  131. return @this.Split(new[] { separator }, option);
  132. }
  133. public static Boolean IsMatch(this string input, string pattern)
  134. {
  135. return Regex.IsMatch(input, pattern);
  136. }
  137. public static Boolean IsMatch(this string input, string pattern, RegexOptions options)
  138. {
  139. return Regex.IsMatch(input, pattern, options);
  140. }
  141. public static Match Match(this string input, string pattern)
  142. {
  143. return Regex.Match(input, pattern);
  144. }
  145. public static Match Match(this string input, string pattern, RegexOptions options)
  146. {
  147. return Regex.Match(input, pattern, options);
  148. }
  149. public static MatchCollection Matches(this string input, string pattern)
  150. {
  151. return Regex.Matches(input, pattern);
  152. }
  153. public static MatchCollection Matches(this string input, string pattern, RegexOptions options)
  154. {
  155. return Regex.Matches(input, pattern, options);
  156. }
  157. }
  158. }