StringExtension.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace SuperSocket.Common
  6. {
  7. /// <summary>
  8. /// String extension class
  9. /// </summary>
  10. public static partial class StringExtension
  11. {
  12. /// <summary>
  13. /// Convert string to int32.
  14. /// </summary>
  15. /// <param name="source">The source.</param>
  16. /// <returns></returns>
  17. public static int ToInt32(this string source)
  18. {
  19. return source.ToInt32(0);
  20. }
  21. /// <summary>
  22. /// Convert string to int32.
  23. /// </summary>
  24. /// <param name="source">The source.</param>
  25. /// <param name="defaultValue">The default value.</param>
  26. /// <returns></returns>
  27. public static int ToInt32(this string source, int defaultValue)
  28. {
  29. if (string.IsNullOrEmpty(source))
  30. return defaultValue;
  31. int value;
  32. if (!int.TryParse(source, out value))
  33. value = defaultValue;
  34. return value;
  35. }
  36. /// <summary>
  37. /// Convert string to long.
  38. /// </summary>
  39. /// <param name="source">The source.</param>
  40. /// <returns></returns>
  41. public static long ToLong(this string source)
  42. {
  43. return source.ToLong(0);
  44. }
  45. /// <summary>
  46. /// Convert string to long.
  47. /// </summary>
  48. /// <param name="source">The source.</param>
  49. /// <param name="defaultValue">The default value.</param>
  50. /// <returns></returns>
  51. public static long ToLong(this string source, long defaultValue)
  52. {
  53. if (string.IsNullOrEmpty(source))
  54. return defaultValue;
  55. long value;
  56. if (!long.TryParse(source, out value))
  57. value = defaultValue;
  58. return value;
  59. }
  60. /// <summary>
  61. /// Convert string to short.
  62. /// </summary>
  63. /// <param name="source">The source.</param>
  64. /// <returns></returns>
  65. public static short ToShort(this string source)
  66. {
  67. return source.ToShort(0);
  68. }
  69. /// <summary>
  70. /// Convert string to short.
  71. /// </summary>
  72. /// <param name="source">The source.</param>
  73. /// <param name="defaultValue">The default value.</param>
  74. /// <returns></returns>
  75. public static short ToShort(this string source, short defaultValue)
  76. {
  77. if (string.IsNullOrEmpty(source))
  78. return defaultValue;
  79. short value;
  80. if (!short.TryParse(source, out value))
  81. value = defaultValue;
  82. return value;
  83. }
  84. /// <summary>
  85. /// Convert string to decimal.
  86. /// </summary>
  87. /// <param name="source">The source.</param>
  88. /// <returns></returns>
  89. public static decimal ToDecimal(this string source)
  90. {
  91. return source.ToDecimal(0);
  92. }
  93. /// <summary>
  94. /// Convert string to decimal.
  95. /// </summary>
  96. /// <param name="source">The source.</param>
  97. /// <param name="defaultValue">The default value.</param>
  98. /// <returns></returns>
  99. public static decimal ToDecimal(this string source, decimal defaultValue)
  100. {
  101. if (string.IsNullOrEmpty(source))
  102. return defaultValue;
  103. decimal value;
  104. if (!decimal.TryParse(source, out value))
  105. value = defaultValue;
  106. return value;
  107. }
  108. /// <summary>
  109. /// Convert string to date time.
  110. /// </summary>
  111. /// <param name="source">The source.</param>
  112. /// <returns></returns>
  113. public static DateTime ToDateTime(this string source)
  114. {
  115. return source.ToDateTime(DateTime.MinValue);
  116. }
  117. /// <summary>
  118. /// Convert string to date time.
  119. /// </summary>
  120. /// <param name="source">The source.</param>
  121. /// <param name="defaultValue">The default value.</param>
  122. /// <returns></returns>
  123. public static DateTime ToDateTime(this string source, DateTime defaultValue)
  124. {
  125. if (string.IsNullOrEmpty(source))
  126. return defaultValue;
  127. DateTime value;
  128. if (!DateTime.TryParse(source, out value))
  129. value = defaultValue;
  130. return value;
  131. }
  132. /// <summary>
  133. /// Convert string to boolean.
  134. /// </summary>
  135. /// <param name="source">The source.</param>
  136. /// <returns></returns>
  137. public static bool ToBoolean(this string source)
  138. {
  139. return source.ToBoolean(false);
  140. }
  141. /// <summary>
  142. /// Convert string tp boolean.
  143. /// </summary>
  144. /// <param name="source">The source.</param>
  145. /// <param name="defaultValue">if set to <c>true</c> [default value].</param>
  146. /// <returns></returns>
  147. public static bool ToBoolean(this string source, bool defaultValue)
  148. {
  149. if (string.IsNullOrEmpty(source))
  150. return defaultValue;
  151. bool value;
  152. if (!bool.TryParse(source, out value))
  153. value = defaultValue;
  154. return value;
  155. }
  156. }
  157. }