| | 1 | | using System; |
| | 2 | |
|
| | 3 | | namespace ArbitraryExtensions.Core |
| | 4 | | { |
| | 5 | | public static class StringExtensions |
| | 6 | | { |
| | 7 | | /// <summary>Checks if the provided input is null or empty and returns the provided default value instead.</summ |
| | 8 | | /// <param name="input">the value to check</param> |
| | 9 | | /// <param name="defaultValue">the default value to return if input is null or empty, optional.</param> |
| | 10 | | /// <returns>input if it's not null or empty, otherwise the provided default value.</returns> |
| 8 | 11 | | public static string DefaultIfNullOrEmpty(this string input, string defaultValue = default) => string.IsNullOrEm |
| | 12 | |
|
| | 13 | | /// <summary>Checks if the provided input is null or whitespace and returns the provided default value instead.< |
| | 14 | | /// <param name="input">the value to check</param> |
| | 15 | | /// <param name="defaultValue">optional, the default value to return if input is null or whitespace.</param> |
| | 16 | | /// <returns>input if it's not null or whitespace, otherwise the provided default value.</returns> |
| 14 | 17 | | public static string DefaultIfNullOrWhitespace(this string input, string defaultValue = default) => string.IsNul |
| | 18 | |
|
| | 19 | | /// <summary>Checks the provided input string and returns string.Empty if null</summary> |
| | 20 | | /// <param name="input">the value to check</param> |
| | 21 | | /// <returns>string.Empty if input is null, else the provided input</returns> |
| 4 | 22 | | public static string EmptyIfNull(this string input) => input ?? string.Empty; |
| | 23 | |
|
| | 24 | | /// <summary>Converts the provided text into title case.</summary> |
| | 25 | | /// <param name="text">the input text to transform</param> |
| | 26 | | /// <param name="includeAllCaps">set this flag to transform abbreviations and all caps input, defaults to false< |
| | 27 | | /// <returns>the provided text input to title case</returns> |
| | 28 | | public static string ToTitleCase(this string text, bool includeAllCaps = false) |
| 4 | 29 | | { |
| 5 | 30 | | if (string.IsNullOrWhiteSpace(text)) return text; |
| 3 | 31 | | var textInfo = System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo; |
| 3 | 32 | | return textInfo.ToTitleCase(includeAllCaps ? text.ToLower() : text); |
| 4 | 33 | | } |
| | 34 | |
|
| | 35 | | /// <summary> |
| | 36 | | /// Compares the provided inputs, ignoring case |
| | 37 | | /// </summary> |
| | 38 | | /// <param name="value">string input</param> |
| | 39 | | /// <param name="compareTo">string to compare</param> |
| | 40 | | /// <returns>True, if strings match ignoring the case, otherwise false</returns> |
| | 41 | | public static bool EqualsIgnoreCase(this string value, string compareTo) |
| 3 | 42 | | { |
| 3 | 43 | | return string.Compare(value, compareTo, StringComparison.InvariantCultureIgnoreCase) == 0; |
| 3 | 44 | | } |
| | 45 | |
|
| | 46 | | /// <summary> |
| | 47 | | /// Checks if the provided input starts with the suffix value, ignoring case |
| | 48 | | /// </summary> |
| | 49 | | /// <param name="value">the input string</param> |
| | 50 | | /// <param name="prefix">the suffix value</param> |
| | 51 | | /// <returns>True, if value starts with suffix, ignoring case, otherwise False</returns> |
| | 52 | | public static bool StartsWithIgnoreCase(this string value, string prefix) |
| 9 | 53 | | { |
| 13 | 54 | | if (string.IsNullOrEmpty(value) || string.IsNullOrEmpty(prefix)) return false; |
| | 55 | |
|
| 5 | 56 | | return value.StartsWith(prefix, StringComparison.InvariantCultureIgnoreCase); |
| 9 | 57 | | } |
| | 58 | |
|
| | 59 | | /// <summary> |
| | 60 | | /// Checks if the provided input ends with the suffix value, ignoring case |
| | 61 | | /// </summary> |
| | 62 | | /// <param name="value">the input string</param> |
| | 63 | | /// <param name="suffix">the suffix value</param> |
| | 64 | | /// <returns>True, if value ends with suffix, ignoring case, otherwise False</returns> |
| | 65 | | public static bool EndsWithIgnoreCase(this string value, string suffix) |
| 9 | 66 | | { |
| 13 | 67 | | if (string.IsNullOrEmpty(value) || string.IsNullOrEmpty(suffix)) return false; |
| | 68 | |
|
| 5 | 69 | | return value.EndsWith(suffix, StringComparison.InvariantCultureIgnoreCase); |
| 9 | 70 | | } |
| | 71 | |
|
| | 72 | | /// <summary>URL encodes the provided string value</summary> |
| | 73 | | /// <param name="value">the value to URL encode</param> |
| | 74 | | /// <returns>the URL encoded value</returns> |
| 0 | 75 | | public static string UrlEncode(this string value) => System.Net.WebUtility.UrlEncode(value); |
| | 76 | |
|
| | 77 | | /// <summary>URL decodes the provided string value</summary> |
| | 78 | | /// <param name="value">the value to URL decode</param> |
| | 79 | | /// <returns>the URL decoded value</returns> |
| 0 | 80 | | public static string UrlDecode(this string value) => System.Net.WebUtility.UrlDecode(value); |
| | 81 | |
|
| | 82 | | /// <summary>HTML encodes the provided string value</summary> |
| | 83 | | /// <param name="value">the value to HTML encode</param> |
| | 84 | | /// <returns>the HTML encoded value</returns> |
| 0 | 85 | | public static string HtmlEncode(this string value) => System.Net.WebUtility.HtmlEncode(value); |
| | 86 | |
|
| | 87 | | /// <summary>HTML decodes the provided string value</summary> |
| | 88 | | /// <param name="value">the value to HTML decode</param> |
| | 89 | | /// <returns>the HTML decoded value</returns> |
| 0 | 90 | | public static string HtmlDecode(this string value) => System.Net.WebUtility.HtmlDecode(value); |
| | 91 | |
|
| | 92 | |
|
| | 93 | | } |
| | 94 | | } |