| | 1 | | namespace Fluorite.Extensions; |
| | 2 | |
|
| | 3 | | /// <summary> |
| | 4 | | /// Provides extension methods for <see cref="string"/>. |
| | 5 | | /// </summary> |
| | 6 | | public 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 | | { |
| 4 | 28 | | Guard.Against.Null(source); |
| 3 | 29 | | Guard.Against.Null(trimString); |
| | 30 | |
|
| 2 | 31 | | if (source == string.Empty || trimString == string.Empty) |
| | 32 | | { |
| 1 | 33 | | return source; |
| | 34 | | } |
| | 35 | |
|
| 3 | 36 | | while (source.EndsWith(trimString)) |
| | 37 | | { |
| 2 | 38 | | source = source.Substring(0, source.Length - trimString.Length); |
| | 39 | | } |
| | 40 | |
|
| 1 | 41 | | 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 | | { |
| 10 | 64 | | Guard.Against.Null(source); |
| 9 | 65 | | Guard.Against.Null(trimString); |
| | 66 | |
|
| 8 | 67 | | if (source == string.Empty || trimString == string.Empty) |
| | 68 | | { |
| 1 | 69 | | return source; |
| | 70 | | } |
| | 71 | |
|
| 7 | 72 | | if (source.EndsWith(trimString)) |
| | 73 | | { |
| 5 | 74 | | return source.Substring(0, source.Length - trimString.Length); |
| | 75 | | } |
| | 76 | | else |
| | 77 | | { |
| 2 | 78 | | 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 | | { |
| 4 | 102 | | Guard.Against.Null(source); |
| 3 | 103 | | Guard.Against.Null(trimString); |
| | 104 | |
|
| 2 | 105 | | if (source == string.Empty || trimString == string.Empty) |
| | 106 | | { |
| 1 | 107 | | return source; |
| | 108 | | } |
| | 109 | |
|
| 1 | 110 | | var result = source; |
| 3 | 111 | | while (result.StartsWith(trimString)) |
| | 112 | | { |
| 2 | 113 | | result = result.Substring(trimString.Length); |
| | 114 | | } |
| | 115 | |
|
| 1 | 116 | | 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 | | { |
| 8 | 139 | | Guard.Against.Null(source); |
| 7 | 140 | | Guard.Against.Null(trimString); |
| | 141 | |
|
| 6 | 142 | | if (source == string.Empty || trimString == string.Empty) |
| | 143 | | { |
| 1 | 144 | | return source; |
| | 145 | | } |
| | 146 | |
|
| 5 | 147 | | if (source.StartsWith(trimString)) |
| | 148 | | { |
| 3 | 149 | | return source.Substring(trimString.Length); |
| | 150 | | } |
| | 151 | | else |
| | 152 | | { |
| 2 | 153 | | return source; |
| | 154 | | } |
| | 155 | | } |
| | 156 | | } |