diff options
| author | Vasily <JustAMan@users.noreply.github.com> | 2020-04-29 12:17:01 +0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-04-29 12:17:01 +0300 |
| commit | 5922c35d692b50817ccbc2acd4e521aab11dd230 (patch) | |
| tree | 92f0a7a72bd96d4b4e153f11219379b70afed5aa /MediaBrowser.Common/Extensions/StringExtensions.cs | |
| parent | 8607b6a9975b2738886ecc4c799f0663b2c1bb24 (diff) | |
| parent | c430a7ed8faa40788c32b89852310981b7c1cf83 (diff) | |
Merge pull request #2920 from Bond-009/tests2
Add some simple tests
Diffstat (limited to 'MediaBrowser.Common/Extensions/StringExtensions.cs')
| -rw-r--r-- | MediaBrowser.Common/Extensions/StringExtensions.cs | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/MediaBrowser.Common/Extensions/StringExtensions.cs b/MediaBrowser.Common/Extensions/StringExtensions.cs new file mode 100644 index 000000000..764301741 --- /dev/null +++ b/MediaBrowser.Common/Extensions/StringExtensions.cs @@ -0,0 +1,37 @@ +#nullable enable + +using System; + +namespace MediaBrowser.Common.Extensions +{ + /// <summary> + /// Extensions methods to simplify string operations. + /// </summary> + public static class StringExtensions + { + /// <summary> + /// Returns the part on the left of the <c>needle</c>. + /// </summary> + /// <param name="haystack">The string to seek.</param> + /// <param name="needle">The needle to find.</param> + /// <returns>The part left of the <paramref name="needle" />.</returns> + public static ReadOnlySpan<char> LeftPart(this ReadOnlySpan<char> haystack, char needle) + { + var pos = haystack.IndexOf(needle); + return pos == -1 ? haystack : haystack[..pos]; + } + + /// <summary> + /// Returns the part on the left of the <c>needle</c>. + /// </summary> + /// <param name="haystack">The string to seek.</param> + /// <param name="needle">The needle to find.</param> + /// <param name="stringComparison">One of the enumeration values that specifies the rules for the search.</param> + /// <returns>The part left of the <c>needle</c>.</returns> + public static ReadOnlySpan<char> LeftPart(this ReadOnlySpan<char> haystack, ReadOnlySpan<char> needle, StringComparison stringComparison = default) + { + var pos = haystack.IndexOf(needle, stringComparison); + return pos == -1 ? haystack : haystack[..pos]; + } + } +} |
