You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[API Proposal]: ReadOnlySpan<char>.StartsWithAny(SearchValues<string>) and ReadOnlySpan<char>.EndsWithAny(SearchValues<string>) #110115
There currently doesn't seem to be a means of checking whether the beginning or the end of a string equals to any of the values contained in a specified set of values in an optimized way using SearchValues<string> that was introduced in .NET 9. There are other specialized extension APIs involving ReadOnlySpan<char> and SearchValues<string> that were recently added (e.g. IndexOfAny<T>(ReadOnlySpan<char>, SearchValues<string>), ContainsAny(ReadOnlySpan<char>, SearchValues<string>) et al.) but this particular API seems to be missing.
One use case could be if you need to efficiently check whether a string starts with any one of a particular set of prefixes in order to determine whether your application code supports or handles that particular item.
changed the title [-][API Proposal]: ReadOnlySpan<char>.StartsWithAny and ReadOnlySpan<char>.EndsWithAny with SearchValues<string>[/-][+][API Proposal]: ReadOnlySpan<char>.StartsWithAny(SearchValues<string>) and ReadOnlySpan<char>.EndsWithAny(SearchValues<string>)[/+]on Nov 24, 2024
Good question; I actually wasn't aware of that. I assume StartsWithAny would be more common (and therefore more useful) than EndsWithAny so including the latter one would only be to make the API complete.
As a workaround, you can use text.IndexOfAny(values) == 0, which would give you the same answer.
Depending on your inputs, that could be more expensive than intended though (long haystack with no matches).
An example for LastIndexOfAny: When generating API names from Vulkan vk.xml, a lot of names contain the suffix defined in the <tags> and somehow want to strip those.
Activity
dotnet-policy-service commentedon Nov 24, 2024
Tagging subscribers to this area: @dotnet/area-system-memory
See info in area-owners.md if you want to be subscribed.
[-][API Proposal]: ReadOnlySpan<char>.StartsWithAny and ReadOnlySpan<char>.EndsWithAny with SearchValues<string>[/-][+][API Proposal]: ReadOnlySpan<char>.StartsWithAny(SearchValues<string>) and ReadOnlySpan<char>.EndsWithAny(SearchValues<string>)[/+]MihaZupan commentedon Nov 24, 2024
What sort of use cases do you have for
EndsWithAny(note that we also didn't expose aLastIndexOfAnyforSearchValues<string>)?silkfire commentedon Nov 24, 2024
Good question; I actually wasn't aware of that. I assume
StartsWithAnywould be more common (and therefore more useful) thanEndsWithAnyso including the latter one would only be to make the API complete.jkoritzinsky commentedon Dec 1, 2024
Re
StartsWithAny, I have a use case in #104999 where I'd love to use it.I don't currently have a use case for
EndsWithAnythough.MihaZupan commentedon Dec 1, 2024
As a workaround, you can use
text.IndexOfAny(values) == 0, which would give you the same answer.Depending on your inputs, that could be more expensive than intended though (long haystack with no matches).
jeffhandley commentedon Jan 16, 2025
Assigned to @krwq for triage and consultation with @MihaZupan and @stephentoub.
pawchen commentedon Apr 15, 2025
An example for
LastIndexOfAny: When generating API names from Vulkan vk.xml, a lot of names contain the suffix defined in the<tags>and somehow want to strip those.xeromorph commentedon Jun 10, 2025
another use case for
LastIndexOfAnyfor us would be checking Uri.Host ends with specific allowed domains, e.g.Ilchert commentedon Feb 27, 2026
One more use case is filtering files by extension