< Summary

Information
Class: Fluorite.Extensions.StringExtensions
Assembly: Fluorite.Strainer
File(s): /builds/fluorite/strainer/src/Strainer/Extensions/StringExtensions.cs
Line coverage
100%
Covered lines: 29
Uncovered lines: 0
Coverable lines: 29
Total lines: 156
Line coverage: 100%
Branch coverage
100%
Covered branches: 24
Total branches: 24
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
TrimEnd(...)100%66100%
TrimEndOnce(...)100%66100%
TrimStart(...)100%66100%
TrimStartOnce(...)100%66100%

File(s)

/builds/fluorite/strainer/src/Strainer/Extensions/StringExtensions.cs

#LineLine coverage
 1namespace Fluorite.Extensions;
 2
 3/// <summary>
 4/// Provides extension methods for <see cref="string"/>.
 5/// </summary>
 6public static class StringExtensions
 7{
 8    /// <summary>
 9    /// Trims all trailing occurences of specified string in current string.
 10    /// </summary>
 11    /// <param name="source">
 12    /// Current <see cref="string"/> instance.
 13    /// </param>
 14    /// <param name="trimString">
 15    /// The string value to trim.
 16    /// </param>
 17    /// <returns>
 18    /// Trimmed string value.
 19    /// </returns>
 20    /// <exception cref="ArgumentNullException">
 21    /// <paramref name="source"/> is <see langword="null"/>.
 22    /// </exception>
 23    /// <exception cref="ArgumentNullException">
 24    /// <paramref name="trimString"/> is <see langword="null"/>.
 25    /// </exception>
 26    public static string TrimEnd(this string source, string trimString)
 27    {
 428        Guard.Against.Null(source);
 329        Guard.Against.Null(trimString);
 30
 231        if (source == string.Empty || trimString == string.Empty)
 32        {
 133            return source;
 34        }
 35
 336        while (source.EndsWith(trimString))
 37        {
 238            source = source.Substring(0, source.Length - trimString.Length);
 39        }
 40
 141        return source;
 42    }
 43
 44    /// <summary>
 45    /// Trims only one trailing occurence of specified string in current string.
 46    /// </summary>
 47    /// <param name="source">
 48    /// Current <see cref="string"/> instance.
 49    /// </param>
 50    /// <param name="trimString">
 51    /// The string value to trim.
 52    /// </param>
 53    /// <returns>
 54    /// Trimmed string value.
 55    /// </returns>
 56    /// <exception cref="ArgumentNullException">
 57    /// <paramref name="source"/> is <see langword="null"/>.
 58    /// </exception>
 59    /// <exception cref="ArgumentNullException">
 60    /// <paramref name="trimString"/> is <see langword="null"/>.
 61    /// </exception>
 62    public static string TrimEndOnce(this string source, string trimString)
 63    {
 1064        Guard.Against.Null(source);
 965        Guard.Against.Null(trimString);
 66
 867        if (source == string.Empty || trimString == string.Empty)
 68        {
 169            return source;
 70        }
 71
 772        if (source.EndsWith(trimString))
 73        {
 574            return source.Substring(0, source.Length - trimString.Length);
 75        }
 76        else
 77        {
 278            return source;
 79        }
 80    }
 81
 82    /// <summary>
 83    /// Trims all leading occurences of specified string in current string.
 84    /// </summary>
 85    /// <param name="source">
 86    /// Current <see cref="string"/> instance.
 87    /// </param>
 88    /// <param name="trimString">
 89    /// The string value to trim.
 90    /// </param>
 91    /// <returns>
 92    /// Trimmed string value.
 93    /// </returns>
 94    /// <exception cref="ArgumentNullException">
 95    /// <paramref name="source"/> is <see langword="null"/>.
 96    /// </exception>
 97    /// <exception cref="ArgumentNullException">
 98    /// <paramref name="trimString"/> is <see langword="null"/>.
 99    /// </exception>
 100    public static string TrimStart(this string source, string trimString)
 101    {
 4102        Guard.Against.Null(source);
 3103        Guard.Against.Null(trimString);
 104
 2105        if (source == string.Empty || trimString == string.Empty)
 106        {
 1107            return source;
 108        }
 109
 1110        var result = source;
 3111        while (result.StartsWith(trimString))
 112        {
 2113            result = result.Substring(trimString.Length);
 114        }
 115
 1116        return result;
 117    }
 118
 119    /// <summary>
 120    /// Trims only one leading occurence of specified string in current string.
 121    /// </summary>
 122    /// <param name="source">
 123    /// Current <see cref="string"/> instance.
 124    /// </param>
 125    /// <param name="trimString">
 126    /// The string value to trim.
 127    /// </param>
 128    /// <returns>
 129    /// Trimmed string value.
 130    /// </returns>
 131    /// <exception cref="ArgumentNullException">
 132    /// <paramref name="source"/> is <see langword="null"/>.
 133    /// </exception>
 134    /// <exception cref="ArgumentNullException">
 135    /// <paramref name="trimString"/> is <see langword="null"/>.
 136    /// </exception>
 137    public static string TrimStartOnce(this string source, string trimString)
 138    {
 8139        Guard.Against.Null(source);
 7140        Guard.Against.Null(trimString);
 141
 6142        if (source == string.Empty || trimString == string.Empty)
 143        {
 1144            return source;
 145        }
 146
 5147        if (source.StartsWith(trimString))
 148        {
 3149            return source.Substring(trimString.Length);
 150        }
 151        else
 152        {
 2153            return source;
 154        }
 155    }
 156}