2
0

StringExtensions.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. #nullable enable
  2. using System;
  3. namespace MediaBrowser.Common.Extensions
  4. {
  5. /// <summary>
  6. /// Extensions methods to simplify string operations.
  7. /// </summary>
  8. public static class StringExtensions
  9. {
  10. /// <summary>
  11. /// Returns the part on the left of the <c>needle</c>.
  12. /// </summary>
  13. /// <param name="haystack">The string to seek.</param>
  14. /// <param name="needle">The needle to find.</param>
  15. /// <returns>The part left of the <paramref name="needle" />.</returns>
  16. public static ReadOnlySpan<char> LeftPart(this ReadOnlySpan<char> haystack, char needle)
  17. {
  18. var pos = haystack.IndexOf(needle);
  19. return pos == -1 ? haystack : haystack[..pos];
  20. }
  21. /// <summary>
  22. /// Returns the part on the left of the <c>needle</c>.
  23. /// </summary>
  24. /// <param name="haystack">The string to seek.</param>
  25. /// <param name="needle">The needle to find.</param>
  26. /// <param name="stringComparison">One of the enumeration values that specifies the rules for the search.</param>
  27. /// <returns>The part left of the <c>needle</c>.</returns>
  28. public static ReadOnlySpan<char> LeftPart(this ReadOnlySpan<char> haystack, ReadOnlySpan<char> needle, StringComparison stringComparison = default)
  29. {
  30. var pos = haystack.IndexOf(needle, stringComparison);
  31. return pos == -1 ? haystack : haystack[..pos];
  32. }
  33. }
  34. }