< Summary

Class:C:\Users\Vaibhav\github\arbitrary-extensions\src\ArbitraryExtensions\Core\StringExtensions.cs
Assembly:Default
File(s):C:\Users\Vaibhav\github\arbitrary-extensions\src\ArbitraryExtensions\Core\StringExtensions.cs
Covered lines:19
Uncovered lines:4
Coverable lines:23
Total lines:94
Line coverage:82.6% (19 of 23)
Covered branches:18
Total branches:18
Branch coverage:100% (18 of 18)

Coverage History

File(s)

C:\Users\Vaibhav\github\arbitrary-extensions\src\ArbitraryExtensions\Core\StringExtensions.cs

#LineLine coverage
 1using System;
 2
 3namespace 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>
 811        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>
 1417        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>
 422        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)
 429        {
 530            if (string.IsNullOrWhiteSpace(text)) return text;
 331            var textInfo = System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo;
 332            return textInfo.ToTitleCase(includeAllCaps ? text.ToLower() : text);
 433        }
 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)
 342        {
 343            return string.Compare(value, compareTo, StringComparison.InvariantCultureIgnoreCase) == 0;
 344        }
 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)
 953        {
 1354            if (string.IsNullOrEmpty(value) || string.IsNullOrEmpty(prefix)) return false;
 55
 556            return value.StartsWith(prefix, StringComparison.InvariantCultureIgnoreCase);
 957        }
 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)
 966        {
 1367            if (string.IsNullOrEmpty(value) || string.IsNullOrEmpty(suffix)) return false;
 68
 569            return value.EndsWith(suffix, StringComparison.InvariantCultureIgnoreCase);
 970        }
 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>
 075        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>
 080        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>
 085        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>
 090        public static string HtmlDecode(this string value) => System.Net.WebUtility.HtmlDecode(value);
 91
 92
 93    }
 94}