aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Common/Extensions/StringExtensions.cs
diff options
context:
space:
mode:
authorMark Monteiro <marknr.monteiro@protonmail.com>2020-05-17 13:50:44 -0400
committerMark Monteiro <marknr.monteiro@protonmail.com>2020-05-17 13:50:44 -0400
commit96acd6481efb25c6f23bc9dd92adaa8da637ab0b (patch)
tree6c72c60a99e0376fd17980651b1f0f888270f028 /MediaBrowser.Common/Extensions/StringExtensions.cs
parent0fb78cf54b51843c54e7ff59d191c490a5b196cd (diff)
parent4b4b50f3eedc5be92786cc9d4e0d244999107426 (diff)
Merge branch 'master' into externalid-type
Diffstat (limited to 'MediaBrowser.Common/Extensions/StringExtensions.cs')
-rw-r--r--MediaBrowser.Common/Extensions/StringExtensions.cs37
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];
+ }
+ }
+}