|
2 | 2 |
|
3 | 3 | public static class StringExtensions
|
4 | 4 | {
|
5 |
| - public static string TrimStart(this string source, string trim, StringComparison stringComparison = StringComparison.Ordinal) |
| 5 | + /// <summary>Removes the prefix from the beginning of the string repeatedly until all occurrences are eliminated.</summary> |
| 6 | + /// <param name="source">The string to trim.</param> |
| 7 | + /// <param name="prefix">The prefix string to remove.</param> |
| 8 | + /// <param name="comparison">The 2nd argument of the <see cref="string.StartsWith(string)"/> method.</param> |
| 9 | + /// <returns>A new <see cref="string"/> without the prefix all occurrences.</returns> |
| 10 | + public static string TrimPrefix(this string source, string prefix, StringComparison comparison = StringComparison.Ordinal) |
6 | 11 | {
|
7 |
| - if (source == null) |
| 12 | + if (source == null || string.IsNullOrEmpty(prefix)) |
8 | 13 | {
|
9 |
| - return null; |
| 14 | + return source; |
10 | 15 | }
|
11 | 16 |
|
12 | 17 | var s = source;
|
13 |
| - while (s.StartsWith(trim, stringComparison)) |
| 18 | + while (s.StartsWith(prefix, comparison)) |
14 | 19 | {
|
15 |
| - s = s.Substring(trim.Length); |
| 20 | + s = s[prefix.Length..]; |
16 | 21 | }
|
17 | 22 |
|
18 | 23 | return s;
|
19 | 24 | }
|
20 | 25 |
|
21 |
| - public static string LastCharAsForwardSlash(this string source) |
22 |
| - { |
23 |
| - if (source.EndsWith('/')) |
24 |
| - { |
25 |
| - return source; |
26 |
| - } |
| 26 | + public const char Slash = '/'; |
27 | 27 |
|
28 |
| - return $"{source}/"; |
29 |
| - } |
| 28 | + /// <summary>Ensures that the last char of the string is forward slash, '/'.</summary> |
| 29 | + /// <param name="source">The string to check its last slash char.</param> |
| 30 | + /// <returns>A <see cref="string"/> witl the last forward slash.</returns> |
| 31 | + public static string LastCharAsForwardSlash(this string source) |
| 32 | + => source.EndsWith(Slash) ? source : source + Slash; |
30 | 33 | }
|
0 commit comments