17 using System.Globalization;
28 private static readonly CultureInfo CultureInfo = CultureInfo.InvariantCulture;
29 private static readonly IFormatProvider FormatProvider = CultureInfo;
30 private static readonly StringComparison StringComparison = StringComparison.InvariantCulture;
38 return (T) value.ConvertInvariant(typeof(T));
53 switch (Type.GetTypeCode(conversionType))
60 var convertible = value as IConvertible;
61 if (convertible !=
null)
63 return convertible.ToType(conversionType, FormatProvider);
66 return Convert.ChangeType(value, conversionType, FormatProvider);
71 case TypeCode.Boolean:
72 return Convert.ToBoolean(value, FormatProvider);
75 return Convert.ToChar(value, FormatProvider);
78 return Convert.ToSByte(value, FormatProvider);
81 return Convert.ToByte(value, FormatProvider);
84 return Convert.ToInt16(value, FormatProvider);
87 return Convert.ToUInt16(value, FormatProvider);
90 return Convert.ToInt32(value, FormatProvider);
93 return Convert.ToUInt32(value, FormatProvider);
96 return Convert.ToInt64(value, FormatProvider);
99 return Convert.ToUInt64(value, FormatProvider);
101 case TypeCode.Single:
102 return Convert.ToSingle(value, FormatProvider);
104 case TypeCode.Double:
105 return Convert.ToDouble(value, FormatProvider);
107 case TypeCode.Decimal:
108 return Convert.ToDecimal(value, FormatProvider);
110 case TypeCode.DateTime:
111 return Convert.ToDateTime(value, FormatProvider);
113 case TypeCode.String:
114 return Convert.ToString(value, FormatProvider);
117 return Convert.ChangeType(value, conversionType, FormatProvider);
127 public static string Invariant(FormattableString formattable)
129 return FormattableString.Invariant(formattable);
137 if (convertible ==
null)
142 return convertible.ToString(FormatProvider);
151 if (formattable ==
null)
164 var indexOfColon = format.IndexOfInvariant(
":");
165 if (indexOfColon != -1)
168 var beforeColon = format.Substring(0, indexOfColon);
169 if (
int.TryParse(beforeColon, out padding))
171 return string.Format(FormatProvider, $
"{{0,{format}}}", formattable);
176 return formattable.ToString(format, FormatProvider);
184 return dateTime.ToStringInvariant(
"O");
193 return value.StartsWith(beginning, ignoreCase, CultureInfo);
202 return value.EndsWith(ending, ignoreCase, CultureInfo);
210 return value.IndexOf(character);
217 public static int IndexOfInvariant(
this string value,
string substring,
bool ignoreCase =
false)
219 return value.IndexOf(substring, ignoreCase
220 ? StringComparison.InvariantCultureIgnoreCase
231 return value.LastIndexOf(substring, ignoreCase
232 ? StringComparison.InvariantCultureIgnoreCase
258 if (
string.IsNullOrEmpty(value))
286 return value.IfNotNullOrEmpty(
default(T), func);
295 public static string SafeSubstring(
this string value,
int startIndex,
int length)
297 if (
string.IsNullOrEmpty(value))
302 if (startIndex > value.Length - 1)
307 if (startIndex < - 1)
312 return value.Substring(startIndex, Math.Min(length, value.Length - startIndex));
324 public static string Truncate(
this string value,
int maxLength)
326 if (value.Length > maxLength)
328 return value.Substring(0, maxLength);