using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace SuperSocket.Common { /// /// String extension class /// public static partial class StringExtension { /// /// Convert string to int32. /// /// The source. /// public static int ToInt32(this string source) { return source.ToInt32(0); } /// /// Convert string to int32. /// /// The source. /// The default value. /// public static int ToInt32(this string source, int defaultValue) { if (string.IsNullOrEmpty(source)) return defaultValue; int value; if (!int.TryParse(source, out value)) value = defaultValue; return value; } /// /// Convert string to long. /// /// The source. /// public static long ToLong(this string source) { return source.ToLong(0); } /// /// Convert string to long. /// /// The source. /// The default value. /// public static long ToLong(this string source, long defaultValue) { if (string.IsNullOrEmpty(source)) return defaultValue; long value; if (!long.TryParse(source, out value)) value = defaultValue; return value; } /// /// Convert string to short. /// /// The source. /// public static short ToShort(this string source) { return source.ToShort(0); } /// /// Convert string to short. /// /// The source. /// The default value. /// public static short ToShort(this string source, short defaultValue) { if (string.IsNullOrEmpty(source)) return defaultValue; short value; if (!short.TryParse(source, out value)) value = defaultValue; return value; } /// /// Convert string to decimal. /// /// The source. /// public static decimal ToDecimal(this string source) { return source.ToDecimal(0); } /// /// Convert string to decimal. /// /// The source. /// The default value. /// public static decimal ToDecimal(this string source, decimal defaultValue) { if (string.IsNullOrEmpty(source)) return defaultValue; decimal value; if (!decimal.TryParse(source, out value)) value = defaultValue; return value; } /// /// Convert string to date time. /// /// The source. /// public static DateTime ToDateTime(this string source) { return source.ToDateTime(DateTime.MinValue); } /// /// Convert string to date time. /// /// The source. /// The default value. /// public static DateTime ToDateTime(this string source, DateTime defaultValue) { if (string.IsNullOrEmpty(source)) return defaultValue; DateTime value; if (!DateTime.TryParse(source, out value)) value = defaultValue; return value; } /// /// Convert string to boolean. /// /// The source. /// public static bool ToBoolean(this string source) { return source.ToBoolean(false); } /// /// Convert string tp boolean. /// /// The source. /// if set to true [default value]. /// public static bool ToBoolean(this string source, bool defaultValue) { if (string.IsNullOrEmpty(source)) return defaultValue; bool value; if (!bool.TryParse(source, out value)) value = defaultValue; return value; } } }