diff options
| author | Joshua M. Boniface <joshua@boniface.me> | 2020-06-13 16:28:37 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-06-13 16:28:37 -0400 |
| commit | 0011e8df47380936742302ef40639a4626a780ed (patch) | |
| tree | b286c590e9d1dbc00cf1acbf8e50f90713e8faf8 /MediaBrowser.Common/Extensions/StringExtensions.cs | |
| parent | d975ad155e7857bcbfcf55246fd78dd87ed594ca (diff) | |
| parent | ec3e15db5789b6218482beb488433f41f9a0d8ba (diff) | |
Merge pull request #3332 from crobibero/api-migration
Merge master into api-migration
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]; + } + } +} |
