PageRenderTime 25ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/Assets/Plugins/UnityEssentials/Core/ExtensionMethods/EMString.cs

https://bitbucket.org/jamvillones/rnc
C# | 264 lines | 116 code | 33 blank | 115 comment | 13 complexity | 8556a4316f56e1910992cb936379fae6 MD5 | raw file
Possible License(s): BSD-2-Clause, Apache-2.0
  1. using System;
  2. using System.Globalization;
  3. using System.Net;
  4. using System.Net.Sockets;
  5. using System.Text.RegularExpressions;
  6. using UnityEngine;
  7. namespace UnityEssentials.ExtensionMethods
  8. {
  9. /// <summary>
  10. /// Extension methods to System.String.
  11. /// </summary>
  12. public static class EMString
  13. {
  14. #region Constants
  15. /// <summary>
  16. /// Regex pattern used for checking the validity of email addresses.
  17. /// </summary>
  18. public const string REGEXPATTERN_EMAILADDRESS = @"\A(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)\Z";
  19. #endregion
  20. #region Static Methods
  21. /// <summary>
  22. /// Returns true if the specified string is null or a string.Empty.
  23. /// </summary>
  24. /// <param name="str"></param>
  25. /// <returns></returns>
  26. public static bool IsNullOrEmpty (this string str)
  27. {
  28. return string.IsNullOrEmpty(str);
  29. }
  30. /// <summary>
  31. /// Returns true if the specified string is a valid email address.
  32. /// </summary>
  33. /// <param name="str"></param>
  34. /// <returns></returns>
  35. public static bool IsValidEmailAddress (this string str)
  36. {
  37. if (string.IsNullOrEmpty(str))
  38. return false;
  39. return Regex.IsMatch(str, REGEXPATTERN_EMAILADDRESS, RegexOptions.IgnoreCase);
  40. }
  41. /// <summary>
  42. /// Returns true if the specified string is a valid IPv4 Address.
  43. /// </summary>
  44. /// <param name="str"></param>
  45. /// <returns></returns>
  46. public static bool IsValidIPvAddress (this string str)
  47. {
  48. if (string.IsNullOrEmpty(str))
  49. return false;
  50. IPAddress ipAddress;
  51. return IPAddress.TryParse(str, out ipAddress) && ipAddress.AddressFamily.Equals(AddressFamily.InterNetwork);
  52. }
  53. /// <summary>
  54. /// Returns true if the specified string is a valid IPv6 Address.
  55. /// </summary>
  56. /// <param name="str"></param>
  57. /// <returns></returns>
  58. public static bool IsValidIPv6Address (this string str)
  59. {
  60. if (string.IsNullOrEmpty(str))
  61. return false;
  62. IPAddress ipAddress;
  63. return IPAddress.TryParse(str, out ipAddress) && ipAddress.AddressFamily.Equals(AddressFamily.InterNetworkV6);
  64. }
  65. /// <summary>
  66. /// Returns true if the specified string is a valid http or https url.
  67. /// </summary>
  68. /// <param name="str"></param>
  69. /// <returns></returns>
  70. public static bool IsValidUrl (this string str)
  71. {
  72. if (string.IsNullOrEmpty(str))
  73. return false;
  74. Uri uri;
  75. return Uri.TryCreate(str, UriKind.Absolute, out uri) && (uri.Scheme.Equals(Uri.UriSchemeHttp) || uri.Scheme.Equals(Uri.UriSchemeHttps));
  76. }
  77. /// <summary>
  78. /// Gets the Levenshtein Distance between string and /value/.
  79. /// </summary>
  80. /// <param name="str"></param>
  81. /// <param name="value"></param>
  82. /// <returns></returns>
  83. public static int LevenshteinDistance (this string str, string value)
  84. {
  85. return UEMath.LevenshteinDistance(str, value);
  86. }
  87. /// <summary>
  88. /// Parses string to int, returns /defaultValue/ when parsing fails.
  89. /// </summary>
  90. /// <param name="str"></param>
  91. /// <param name="defaultValue"></param>
  92. /// <returns></returns>
  93. public static int ParseToIntOrDefault (this string str, int defaultValue = default(int))
  94. {
  95. int value;
  96. return (int.TryParse(str, out value)) ? value : defaultValue;
  97. }
  98. /// <summary>
  99. /// Parses string to long, returns /defaultValue/ when parsing fails.
  100. /// </summary>
  101. /// <param name="str"></param>
  102. /// <param name="defaultValue"></param>
  103. /// <returns></returns>
  104. public static long ParseToLongOrDefault (this string str, long defaultValue = default(long))
  105. {
  106. long value;
  107. return (long.TryParse(str, out value)) ? value : defaultValue;
  108. }
  109. /// <summary>
  110. /// Parses string to float, returns /defaultValue/ when parsing fails.
  111. /// </summary>
  112. /// <param name="str"></param>
  113. /// <param name="defaultValue"></param>
  114. /// <returns></returns>
  115. public static float ParseToFloatOrDefault (this string str, float defaultValue = default(float))
  116. {
  117. float value;
  118. return (float.TryParse(str, out value)) ? value : defaultValue;
  119. }
  120. /// <summary>
  121. /// Parses string to double, returns /defaultValue/ when parsing fails.
  122. /// </summary>
  123. /// <param name="str"></param>
  124. /// <param name="defaultValue"></param>
  125. /// <returns></returns>
  126. public static double ParseToDoubleOrDefault (this string str, double defaultValue = default(double))
  127. {
  128. double value;
  129. return (double.TryParse(str, out value)) ? value : defaultValue;
  130. }
  131. /// <summary>
  132. /// Parses string to decimal, returns /defaultValue/ when parsing fails.
  133. /// </summary>
  134. /// <param name="str"></param>
  135. /// <param name="defaultValue"></param>
  136. /// <returns></returns>
  137. public static decimal ParseToDecimalOrDefault (this string str, decimal defaultValue = default(decimal))
  138. {
  139. decimal value;
  140. return (decimal.TryParse(str, out value)) ? value : defaultValue;
  141. }
  142. /// <summary>
  143. /// Replaces the format item in a specified string with the string representation of a corresponding object in a specified array.
  144. /// </summary>
  145. /// <param name="str"></param>
  146. /// <param name="format"></param>
  147. /// <param name="args"></param>
  148. /// <returns></returns>
  149. public static string Format (this string str, string format, params object[] args)
  150. {
  151. return string.Format(format, args);
  152. }
  153. /// <summary>
  154. /// Formats a string with Markup '&lt;color=/color/&gt;/str/&lt;/color&gt;'.
  155. /// </summary>
  156. /// <param name="str"></param>
  157. /// <param name="color"></param>
  158. /// <returns></returns>
  159. public static string RichTextColorize (this string str, Color color)
  160. {
  161. return string.Format("<color={0}>{1}</color>", color.ToHex(), str);
  162. }
  163. /// <summary>
  164. /// Formats a string with Markup '&lt;b&gt;/str/&lt;b&gt;'.
  165. /// </summary>
  166. /// <param name="str"></param>
  167. /// <returns></returns>
  168. public static string RichTextBolden (this string str)
  169. {
  170. return string.Format("<b>{0}</b>", str);
  171. }
  172. /// <summary>
  173. /// Formats a string with Markup '&lt;i&gt;/str/&lt;i&gt;'.
  174. /// </summary>
  175. /// <param name="str"></param>
  176. /// <returns></returns>
  177. public static string RichTextItalicize (this string str)
  178. {
  179. return string.Format("<i>{0}</i>", str);
  180. }
  181. /// <summary>
  182. /// Formats a string with Markup '&lt;size=/size/&gt;/str/&lt;/size&gt;'.
  183. /// </summary>
  184. /// <param name="str"></param>
  185. /// <param name="size"></param>
  186. /// <returns></returns>
  187. public static string RichTextResize (this string str, int size)
  188. {
  189. return string.Format("<size={0}>{1}</size>", size, str);
  190. }
  191. /// <summary>
  192. /// Converts the specified string to title case (except for words that are entirely in uppercase, which are considered to be acronyms).
  193. /// </summary>
  194. /// <param name="str"></param>
  195. /// <returns></returns>
  196. public static string ToTitleCase (this string str)
  197. {
  198. return CultureInfo.CurrentCulture.TextInfo.ToTitleCase(str.ToLower());
  199. }
  200. /// <summary>
  201. /// Shortens string down to /length/.
  202. /// </summary>
  203. /// <param name="str"></param>
  204. /// <param name="length"></param>
  205. /// <param name="addEllipsis"></param>
  206. /// <param name="ellipsis"></param>
  207. /// <returns></returns>
  208. public static string Truncate (this string str, int length, bool addEllipsis = true, string ellipsis = "...")
  209. {
  210. if (length <= 0)
  211. throw new ArgumentException("Cannot be less than or equal to zero.", "length");
  212. if (string.IsNullOrEmpty(str) || str.Length <= length)
  213. return str;
  214. if (addEllipsis && length <= ellipsis.Length)
  215. addEllipsis = false;
  216. string truncated = str.Substring(0, Mathf.Clamp((length - (addEllipsis ? ellipsis.Length : 0)), 0, str.Length)) + (addEllipsis ? ellipsis : string.Empty);
  217. return truncated;
  218. }
  219. /// <summary>
  220. /// Converts string to enum.
  221. /// </summary>
  222. /// <typeparam name="T"></typeparam>
  223. /// <param name="str"></param>
  224. /// <returns></returns>
  225. public static T ToEnum <T> (this string str) where T : struct
  226. {
  227. return (T) Enum.Parse(typeof(T), str, true);
  228. }
  229. #endregion
  230. }
  231. }