using System; using System.Text.RegularExpressions; namespace Extensions { public static class stringExtension { public static T ToJson(this string obj) { return Newtonsoft.Json.JsonConvert.DeserializeObject(obj); } public static bool IsNullOrEmpty(this string @this) { return string.IsNullOrEmpty(@this); } public static Nullable ToNullable(this string @this) where T:struct { if (string.IsNullOrWhiteSpace(@this)) return null; var val=Convert.ChangeType(@this, typeof(T)); return (T)val; } public static bool IsNotNullOrEmpty(this string @this) { return !string.IsNullOrEmpty(@this); } public static bool IsNullOrWhitespace(this string @this) { return string.IsNullOrWhiteSpace(@this); } public static bool IsNotNullOrWhitespace(this string @this) { return !string.IsNullOrWhiteSpace(@this); } public static string IfNullOrEmpty(this string @this, string defaultVal) { return string.IsNullOrEmpty(@this) ? defaultVal : @this; } public static string IfNullOrWhitespace(this string @this, string defaultVal) { return string.IsNullOrWhiteSpace(@this) ? defaultVal : @this; } public static string ClearWhitespace(this string @this) { if (string.IsNullOrWhiteSpace(@this)) return string.Empty; return @this.Replace(" ", string.Empty); } public static string Left(this string @this, int length) { if (string.IsNullOrEmpty(@this)) return string.Empty; return @this.Length < length ? @this : @this.Substring(0, length); } public static string Rigth(this string @this, int length) { if (string.IsNullOrEmpty(@this)) return string.Empty; return @this.Length < length ? @this : @this.Substring(@this.Length - length); } public static string TrimToMaxLength(this string @this, int maxLength, string suffix = "") { return ((@this == null || @this.Length <= maxLength) ? @this : string.Concat(@this.Substring(0, maxLength), suffix)); } public static T To(this string @this) { return To(@this, default(T)); } public static T To(this string @this, T defaultValue) { if (string.IsNullOrWhiteSpace(@this)) return defaultValue; T retVal = defaultValue; try { //获取要转换的目标类型 Type targetType = typeof(T); //对 Guid 类型的值进行单独处理 if (targetType == typeof(Guid)) retVal = (T)((object)(new Guid(@this))); //对 Enum 类型的值进行单独处理 else if (targetType.BaseType == typeof(Enum)) retVal = (T)Enum.Parse(targetType, @this); else retVal = (T)Convert.ChangeType(@this, targetType); } catch { } return retVal; } public static string GetBefore(this string value, string x) { if (value == null || x == null) return string.Empty; int xPos = value.IndexOf(x); return xPos == -1 ? string.Empty : value.Substring(0, xPos); } public static string GetAfter(this string value, string x) { if (value == null || x == null) return string.Empty; int xPos = value.LastIndexOf(x); if (xPos == -1) return string.Empty; int startIndex = xPos + x.Length; return startIndex >= value.Length ? string.Empty : value.Substring(startIndex); } public static string GetBetween(this string value, string x, string y) { if (value == null || x == null || y == null) return string.Empty; int xPos = value.IndexOf(x); int yPos = value.LastIndexOf(y); if (xPos == -1 || yPos == -1) return string.Empty; int startIndex = xPos + x.Length; return startIndex >= yPos ? string.Empty : value.Substring(startIndex, yPos - startIndex); } public static string FormatWith(this string @this, Object arg0) { return string.Format(@this, arg0); } public static string FormatWith(this string @this, Object arg0, Object arg1) { return string.Format(@this, arg0, arg1); } public static string FormatWith(this string @this, Object arg0, Object arg1, Object arg2) { return string.Format(@this, arg0, arg1, arg2); } public static string FormatWith(this string @this, params object[] values) { return string.Format(@this, values); } public static string[] Split(this string @this, string separator, StringSplitOptions option = StringSplitOptions.None) { return @this.Split(new[] { separator }, option); } public static Boolean IsMatch(this string input, string pattern) { return Regex.IsMatch(input, pattern); } public static Boolean IsMatch(this string input, string pattern, RegexOptions options) { return Regex.IsMatch(input, pattern, options); } public static Match Match(this string input, string pattern) { return Regex.Match(input, pattern); } public static Match Match(this string input, string pattern, RegexOptions options) { return Regex.Match(input, pattern, options); } public static MatchCollection Matches(this string input, string pattern) { return Regex.Matches(input, pattern); } public static MatchCollection Matches(this string input, string pattern, RegexOptions options) { return Regex.Matches(input, pattern, options); } } }