You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
jellyfin/MediaBrowser.Common/Extensions/StringExtensions.cs

38 lines
1.5 KiB

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