jellyfin-server/MediaBrowser.Common/Extensions/StringExtensions.cs

38 lines
1.5 KiB
C#
Raw Normal View History

2020-04-19 13:18:28 +00:00
#nullable enable
using System;
namespace MediaBrowser.Common.Extensions
{
/// <summary>
/// Extensions methods to simplify string operations.
/// </summary>
public static class StringExtensions
{
/// <summary>
2020-04-21 08:18:26 +00:00
/// Returns the part on the left of the <c>needle</c>.
2020-04-19 13:18:28 +00:00
/// </summary>
2020-04-21 08:18:26 +00:00
/// <param name="haystack">The string to seek.</param>
2020-04-19 13:18:28 +00:00
/// <param name="needle">The needle to find.</param>
2020-04-21 08:18:26 +00:00
/// <returns>The part left of the <paramref name="needle" />.</returns>
public static ReadOnlySpan<char> LeftPart(this ReadOnlySpan<char> haystack, char needle)
2020-04-19 13:18:28 +00:00
{
2020-04-21 08:18:26 +00:00
var pos = haystack.IndexOf(needle);
return pos == -1 ? haystack : haystack[..pos];
2020-04-19 13:18:28 +00:00
}
/// <summary>
2020-04-21 08:18:26 +00:00
/// Returns the part on the left of the <c>needle</c>.
2020-04-19 13:18:28 +00:00
/// </summary>
2020-04-21 08:18:26 +00:00
/// <param name="haystack">The string to seek.</param>
2020-04-19 13:18:28 +00:00
/// <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>
2020-04-21 08:18:26 +00:00
public static ReadOnlySpan<char> LeftPart(this ReadOnlySpan<char> haystack, ReadOnlySpan<char> needle, StringComparison stringComparison = default)
2020-04-19 13:18:28 +00:00
{
2020-04-21 08:18:26 +00:00
var pos = haystack.IndexOf(needle, stringComparison);
return pos == -1 ? haystack : haystack[..pos];
2020-04-19 13:18:28 +00:00
}
}
}