123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace SuperSocket.Common
- {
- /// <summary>
- /// String extension class
- /// </summary>
- public static partial class StringExtension
- {
- /// <summary>
- /// Convert string to int32.
- /// </summary>
- /// <param name="source">The source.</param>
- /// <returns></returns>
- public static int ToInt32(this string source)
- {
- return source.ToInt32(0);
- }
- /// <summary>
- /// Convert string to int32.
- /// </summary>
- /// <param name="source">The source.</param>
- /// <param name="defaultValue">The default value.</param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// Convert string to long.
- /// </summary>
- /// <param name="source">The source.</param>
- /// <returns></returns>
- public static long ToLong(this string source)
- {
- return source.ToLong(0);
- }
- /// <summary>
- /// Convert string to long.
- /// </summary>
- /// <param name="source">The source.</param>
- /// <param name="defaultValue">The default value.</param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// Convert string to short.
- /// </summary>
- /// <param name="source">The source.</param>
- /// <returns></returns>
- public static short ToShort(this string source)
- {
- return source.ToShort(0);
- }
- /// <summary>
- /// Convert string to short.
- /// </summary>
- /// <param name="source">The source.</param>
- /// <param name="defaultValue">The default value.</param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// Convert string to decimal.
- /// </summary>
- /// <param name="source">The source.</param>
- /// <returns></returns>
- public static decimal ToDecimal(this string source)
- {
- return source.ToDecimal(0);
- }
- /// <summary>
- /// Convert string to decimal.
- /// </summary>
- /// <param name="source">The source.</param>
- /// <param name="defaultValue">The default value.</param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// Convert string to date time.
- /// </summary>
- /// <param name="source">The source.</param>
- /// <returns></returns>
- public static DateTime ToDateTime(this string source)
- {
- return source.ToDateTime(DateTime.MinValue);
- }
- /// <summary>
- /// Convert string to date time.
- /// </summary>
- /// <param name="source">The source.</param>
- /// <param name="defaultValue">The default value.</param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// Convert string to boolean.
- /// </summary>
- /// <param name="source">The source.</param>
- /// <returns></returns>
- public static bool ToBoolean(this string source)
- {
- return source.ToBoolean(false);
- }
- /// <summary>
- /// Convert string tp boolean.
- /// </summary>
- /// <param name="source">The source.</param>
- /// <param name="defaultValue">if set to <c>true</c> [default value].</param>
- /// <returns></returns>
- 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;
- }
- }
- }
|