Home
C#
String IndexOfAny Examples
Updated Nov 15, 2023
Dot Net Perls
IndexOfAny. This C# method searches a string for many values. It returns the first position of any of the values you provide (or -1).
String IndexOf
Several overloads of the IndexOfAny method are available in .NET. This method is useful in certain C# programs—its performance is not great.
Some examples. IndexOfAny is an instance method that returns an integer value type indicating the position of the found character, or the special value -1 if no value was found.
And The first argument is a character array, which you can declare directly in the parameter list or separately as a variable.
Tip It is sometimes faster to store this char array separately and reuse it whenever calling IndexOfAny.
char Array
Detail The IndexOfAny method is called and searches for the lowercase "c" and uppercase "Z" in the 2 input strings.
using System; // Input. const string value1 = "abc"; const string value2 = "XYZ"; // Find first location of "c" or "Z." int index1 = value1.IndexOfAny(new char[] { 'c', 'Z' }); Console.WriteLine(index1); // Repeat on another string. int index2 = value2.IndexOfAny(new char[] { 'c', 'Z' }); Console.WriteLine(index2);
2 2
IndexOfAny benchmark. Consider now the performance of IndexOfAny. Can this method be used in performance-critical string methods? We test its performance.
Version 1 Use IndexOfAny with a new char array allocation as the argument, and test the result.
Version 2 Use a custom for-loop with an if-statement and several conditions in the logic.
Result The IndexOfAny method seems to perform much worse than a for-loop with an if-statement. For a simple search, it is far slower.
for
if
However If an array of characters is needed, the IndexOfAny method's performance will likely be more competitive.
using System; using System.Diagnostics; const int _max = 1000000; string test = "abcdefg"; // Version 1: test IndexOfAny. var s1 = Stopwatch.StartNew(); for (int i = 0; i < _max; i++) { int result = test.IndexOfAny(new char[] { 'd', 'x', '?' }); if (result != 3) { return; } } s1.Stop(); // Version 2: test custom loop with if-statement. var s2 = Stopwatch.StartNew(); for (int i = 0; i < _max; i++) { int result = -1; for (int x = 0; x < test.Length; x++) { if (test[x] == 'd' || test[x] == 'x' || test[x] == '?') { result = x; break; } } if (result != 3) { return; } } s2.Stop(); Console.WriteLine(s1.Elapsed.TotalMilliseconds); Console.WriteLine(s2.Elapsed.TotalMilliseconds);
36.7644 ms IndexOfAny 6.175 ms For-loop
Notes, forward-only. IndexOfAny() always performs a forward-only (left to right) scan of the input string. You can use the LastIndexOfAny method for a reversed scan.
String LastIndexOf
IndexOfAny provides a way to scan an input string declaratively for the first occurrence of any of the characters in the parameter char array. This method reduces loop nesting.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Nov 15, 2023 (edit).
Home
Changes
© 2007-2025 Sam Allen