PageRenderTime 39ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/ExtensionMethods/ExtensionMethods.String.cs

http://github.com/fredericaltorres/DynamicSugarNet
C# | 346 lines | 142 code | 28 blank | 176 comment | 66 complexity | 8f159c02a479ca436aa88cb3abcab27f MD5 | raw file
  1. using System;
  2. using System.Text;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.IO;
  6. using System.Collections;
  7. using System.Globalization;
  8. #if !MONOTOUCH
  9. using System.Dynamic;
  10. #endif
  11. // http://extensionmethod.net/
  12. namespace DynamicSugar {
  13. public static class ExtensionMethods_Format {
  14. public static int ToInt (this string s, int? defaultValue = null) { try { return int.Parse(s); } catch { if(defaultValue == null) throw;return (int)defaultValue; } }
  15. public static uint ToUInt (this string s, uint? defaultValue = null) { try { return uint.Parse(s); } catch { if(defaultValue == null) throw;return defaultValue.Value; } }
  16. public static long ToLong (this string s, long? defaultValue = null) { try { return long.Parse(s); } catch { if(defaultValue == null) throw;return defaultValue.Value; } }
  17. public static ulong ToULong (this string s, ulong? defaultValue = null) { try { return ulong.Parse(s); } catch { if(defaultValue == null) throw;return defaultValue.Value; } }
  18. public static UInt16 ToUInt16 (this string s, UInt16? defaultValue = null) { try { return UInt16.Parse(s); } catch { if(defaultValue == null) throw;return defaultValue.Value; } }
  19. public static Int16 ToInt16 (this string s, Int16? defaultValue = null) { try { return Int16.Parse(s); } catch { if(defaultValue == null) throw;return defaultValue.Value; } }
  20. public static UInt32 ToUInt32 (this string s, UInt32? defaultValue = null) { try { return UInt32.Parse(s); } catch { if(defaultValue == null) throw;return defaultValue.Value; } }
  21. public static Int32 ToInt32 (this string s, Int32? defaultValue = null) { try { return Int32.Parse(s); } catch { if(defaultValue == null) throw;return defaultValue.Value; } }
  22. public static UInt64 ToUInt64 (this string s, UInt64? defaultValue = null) { try { return UInt64.Parse(s); } catch { if(defaultValue == null) throw;return defaultValue.Value; } }
  23. public static Int64 ToInt64 (this string s, Int64? defaultValue = null) { try { return Int64.Parse(s); } catch { if(defaultValue == null) throw;return defaultValue.Value; } }
  24. public static decimal ToDecimal (this string s, decimal? defaultValue = null) { try { return decimal.Parse(s); } catch { if(defaultValue == null) throw;return defaultValue.Value; } }
  25. public static double ToDouble (this string s, double? defaultValue = null) { try { return double.Parse(s); } catch { if(defaultValue == null) throw;return defaultValue.Value; } }
  26. public static DateTime ToDateTime(this string s, DateTime? defaultValue = null) { try { return DateTime.Parse(s); } catch { if(defaultValue == null) throw;return defaultValue.Value; } }
  27. public static Guid ToGuid (this string s, Guid? defaultValue = null) { try { return new Guid(s); } catch { if(defaultValue == null) throw;return defaultValue.Value; } }
  28. public static bool ToBool (this string s, bool? defaultValue = null) { try { return bool.Parse(s); } catch { if(defaultValue == null) throw;return defaultValue.Value; } }
  29. /// <summary>
  30. /// The slice() method selects a part of a string.
  31. /// The original string is not be changed.
  32. /// </summary>
  33. /// <param name="s">the string</param>
  34. /// <param name="index">An integer that specifies where to start the selection
  35. /// (The first element has an index of 0).
  36. /// You can also use negative numbers to select from the end of the string</param>
  37. /// <param name="len">
  38. /// Optional. An integer that specifies how many char to return. If omitted, slice() selects
  39. /// all elements from the start position and to the end of the string.
  40. /// </param>
  41. /// <returns></returns>
  42. public static string Slice(this string s, int index, int len=-1){
  43. int x = index;
  44. bool allDefined = len == -1;
  45. string result = null;
  46. if(x<0){
  47. string _stringReversed = s.Reverse();
  48. var index2 = Math.Abs(x)-1;
  49. if(allDefined)
  50. len = _stringReversed.Length-index2;
  51. if (index2 < _stringReversed.Length)
  52. result = _stringReversed.Substring(index2, len);
  53. else
  54. result = ""; // If the index goes over the limit of the string we return ""
  55. }
  56. else{
  57. if(allDefined)
  58. len = s.Length-x;
  59. if(x<s.Length)
  60. result = s.Substring(x, len);
  61. else
  62. result = ""; // If the index goes over the limit of the string we return ""
  63. }
  64. return result;
  65. }
  66. /// <summary>
  67. /// Capitalize the string
  68. /// </summary>
  69. /// <param name="s"></param>
  70. /// <param name="charToRemove">If defined only remove the first char if it is equal to charToRemove</param>
  71. /// <returns></returns>
  72. public static string Capitalize(this string s, CultureInfo cultureInfo = null)
  73. {
  74. if(s == null)
  75. return null;
  76. if(cultureInfo == null)
  77. cultureInfo = new CultureInfo("en-US", false);
  78. s = s.ToLower(cultureInfo);
  79. var t = cultureInfo.TextInfo;
  80. return s = t.ToTitleCase(s.ToLower(cultureInfo));
  81. }
  82. /// <summary>
  83. /// Remove the first char
  84. /// </summary>
  85. /// <param name="s"></param>
  86. /// <param name="charToRemove">If defined only remove the first char if it is equal to charToRemove</param>
  87. /// <returns></returns>
  88. public static string RemoveFirstChar(this string s, char charToRemove = '\0')
  89. {
  90. if(s == null)
  91. return null;
  92. if (charToRemove == '\0') {
  93. if(s.Length > 0) {
  94. s = s.Substring(1);
  95. }
  96. }
  97. else {
  98. if(s.Length > 0 && s[0] == charToRemove) {
  99. s = s.Substring(1);
  100. }
  101. }
  102. return s;
  103. }
  104. /// <summary>
  105. /// Remove a piece of the string at the start of the string
  106. /// </summary>
  107. /// <param name="s"></param>
  108. /// <param name="startWithString"></param>
  109. /// <returns></returns>
  110. public static string RemoveIfStartsWith(this string s, string startWithString)
  111. {
  112. if(string.IsNullOrEmpty(s) || string.IsNullOrEmpty(startWithString))
  113. return s;
  114. if(s.StartsWith(startWithString)) {
  115. s = s.Substring(startWithString.Length);
  116. }
  117. return s;
  118. }
  119. public static string RemoveIfEndsWith(this string s, string startWithString)
  120. {
  121. if(string.IsNullOrEmpty(s) || string.IsNullOrEmpty(startWithString))
  122. return s;
  123. if(s.EndsWith(startWithString)) {
  124. s = s.Substring(0, s.Length-startWithString.Length);
  125. }
  126. return s;
  127. }
  128. /// <summary>
  129. /// Remove the first char
  130. /// </summary>
  131. /// <param name="s"></param>
  132. /// <param name="charToRemove">If defined only remove the first char if it is equal to charToRemove</param>
  133. /// <returns></returns>
  134. public static string RemoveLastChar(this string s, char charToRemove = '\0')
  135. {
  136. if(s == null)
  137. return null;
  138. if (charToRemove == '\0') {
  139. if(s.Length > 0) {
  140. s = s.Substring(0, s.Length-1);
  141. }
  142. }
  143. else {
  144. if(s.Length > 0 && s[s.Length-1] == charToRemove) {
  145. s = s.Substring(0, s.Length-1);
  146. }
  147. }
  148. return s;
  149. }
  150. /// <summary>
  151. /// Return the string reversed.
  152. /// </summary>
  153. /// <param name="s"></param>
  154. /// <returns></returns>
  155. public static string Reverse(this string s) {
  156. char[] arr = s.ToCharArray();
  157. Array.Reverse(arr);
  158. return new string(arr);
  159. }
  160. ///// <summary>
  161. ///// Concatenates the elements of an object array, using the specified separator
  162. //// between each element.
  163. ///// </summary>
  164. ///// <param name="s"></param>
  165. ///// <param name="values">An array that contains the elements to concatenate.</param>
  166. ///// <returns>A string that consists of the elements of values delimited by the separator
  167. ///// string.</returns>
  168. //public static string join(this string s, params object[] values) {
  169. // return string.Join(s, values);
  170. //}
  171. /// <summary>
  172. /// Concatenates the elements of a List Of T, using the specified separator
  173. /// between each element.
  174. /// </summary>
  175. /// <param name="s"></param>
  176. /// <param name="values">A List Of T contains the elements to concatenate.</param>
  177. /// <returns>A string that consists of the elements of values delimited by the separator
  178. /// string.</returns>
  179. public static string Join<T>(this string s, List<T> values) {
  180. return string.Join(s, values.ToArray());
  181. }
  182. /// <summary>
  183. /// Return the value of the string <paramref name="s"/> if not null
  184. /// or empty else return the default value.
  185. /// </summary>
  186. /// <param name="s"></param>
  187. /// <param name="defaultValue"></param>
  188. /// <returns></returns>
  189. public static string IfNullOrEmpty(this string s, string defaultValue)
  190. {
  191. return string.IsNullOrEmpty(s) ? defaultValue : s;
  192. }
  193. /// <summary>
  194. /// Return the value of the string <paramref name="s"/> if not null
  195. /// else return the default value.
  196. /// </summary>
  197. /// <param name="s"></param>
  198. /// <param name="defaultValue"></param>
  199. /// <returns></returns>
  200. public static string IfNull(this string s, string defaultValue)
  201. {
  202. return s == null ? defaultValue : s;
  203. }
  204. /// <summary>
  205. /// Indicates whether the specified string is null or an System.String.Empty
  206. /// string.
  207. /// </summary>
  208. /// <param name="s">The string to test</param>
  209. /// <returns>true if the value parameter is null or an empty string (""); otherwise, false.</returns>
  210. public static bool IsNullOrEmpty(this string s) {
  211. return string.IsNullOrEmpty(s);
  212. }
  213. /// <summary>
  214. /// Indicates whether the specified string is an System.String.Empty
  215. /// string.
  216. /// </summary>
  217. /// <param name="s">The string to test</param>
  218. /// <returns>true if the value parameter is an empty string (""); otherwise, false.</returns>
  219. public static bool IsEmpty(this string s) {
  220. return (s!=null) && s.Length==0;
  221. }
  222. /// <summary>
  223. /// Replaces the FormatString item in the string with the string representation
  224. /// of a corresponding object in a specified array.
  225. /// </summary>
  226. /// <param name="s"></param>
  227. /// <param name="args">An object array that contains zero or more objects to FormatString</param>
  228. /// <returns>A copy of FormatString in which the FormatString items have been replaced by the string
  229. /// representation of the corresponding objects in args.
  230. /// </returns>
  231. public static string FormatString(this string s, params object[] args)
  232. {
  233. return string.Format(s, args);
  234. }
  235. //public static string Format(this string s, object a1) { return string.Format(s, a1); }
  236. //public static string Format(this string s, object a1, object a2) { return string.Format(s, a1, a2); }
  237. //public static string Format(this string s, object a1, object a2, object a3) { return string.Format(s, a1, a2, a3); }
  238. //public static string Format(this string s, object a1, object a2, object a3, object a4) { return string.Format(s, a1, a2, a3, a4); }
  239. //public static string Format(this string s, object a1, object a2, object a3, object a4, object a5) { return string.Format(s, a1, a2, a3, a4, a5); }
  240. //public static string Format(this string s, object a1, object a2, object a3, object a4, object a5, object a6) { return string.Format(s, a1, a2, a3, a4, a5, a6); }
  241. //public static string Format(this string s, object a1, object a2, object a3, object a4, object a5, object a6, object a7) { return string.Format(s, a1, a2, a3, a4, a5, a6, a7); }
  242. //public static string Format(this string s, object a1, object a2, object a3, object a4, object a5, object a6, object a7, object a8) { return string.Format(s, a1, a2, a3, a4, a5, a6, a7, a8); }
  243. //public static string Format(this string s, object a1, object a2, object a3, object a4, object a5, object a6, object a7, object a8, object a9) { return string.Format(s, a1, a2, a3, a4, a5, a6, a7, a8, a9); }
  244. //public static string Format(this string s, object a1, object a2, object a3, object a4, object a5, object a6, object a7, object a8, object a9, object a10) { return string.Format(s, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10); }
  245. /*
  246. /// <summary>
  247. /// http://extensionmethod.net/csharp/string/FormatString-string
  248. /// Author: Adam Weigert
  249. /// </summary>
  250. /// <param name="FormatString"></param>
  251. /// <param name="arg"></param>
  252. /// <param name="additionalArgs"></param>
  253. /// <returns></returns>
  254. public static string Formataaaaa(this string FormatString, object arg, params object[] additionalArgs)
  255. {
  256. if (additionalArgs == null || additionalArgs.Length == 0)
  257. {
  258. return string.Format(FormatString, arg);
  259. }
  260. else
  261. {
  262. return string.Format(FormatString, new object[] { arg }.Concat(additionalArgs).ToArray());
  263. }
  264. }*/
  265. /// <summary>
  266. /// Replaces the FormatString item in the string with the string representation
  267. /// of a corresponding property/field in the object passed.
  268. /// </summary>
  269. /// <param name="s"></param>
  270. /// <param name="poco">Any poco object</param>
  271. /// <returns></returns>
  272. public static string Template(this string s, object poco) {
  273. return ExtendedFormat.Format(s, ReflectionHelper.GetDictionary(poco));
  274. }
  275. }
  276. }
  277. /*
  278. * public static string Slice(this string _string, int index, int len0=int.MinValue){
  279. int len = -1;
  280. int x = index;
  281. bool rangeDefined = len0!=int.MinValue;
  282. bool allDefined = false;
  283. string result = null;
  284. if(rangeDefined){
  285. len = len0;
  286. allDefined = len == -1;
  287. }
  288. if(x<0){
  289. string _stringReversed = _string.Reverse();
  290. var index2 = Math.Abs(x)-1;
  291. if(rangeDefined){
  292. if(allDefined)
  293. len = _stringReversed.Length-index2;
  294. if (index2 < _stringReversed.Length)
  295. result = _stringReversed.Substring(index2, len);
  296. else
  297. result = ""; // If the index goes over the limit of the string we return ""
  298. }
  299. else
  300. result = _stringReversed[index2].ToString();
  301. }
  302. else{
  303. if(rangeDefined){
  304. if(allDefined)
  305. len = _string.Length-x;
  306. if(x<_string.Length)
  307. result = _string.Substring(x, len);
  308. else
  309. result = ""; // If the index goes over the limit of the string we return ""
  310. }
  311. else
  312. result = _string[x].ToString();
  313. }
  314. return result;
  315. }
  316. */