In C#, IndexOfAny() searches a string for several characters at once. You give it a char[] (a set of characters), and it returns the position of the first character in the string that is in your set. If none of them appear, it returns -1.
Think of it as asking “where is the first comma, dot, or question mark?” in one call instead of three. The order of characters inside your set does not matter; what matters is which one appears first in the text. To search from the end instead, use LastIndexOfAny().
This is an instance member of System.String. Call it on a string variable, such as text.IndexOfAny(...). Calling it on a null variable throws NullReferenceException. It only reads the text. Strings are immutable, so the original string is never changed.
Syntax
Pass a char array. Each character in it is a possible match.
C#
string text = "hello, world!";
char[] marks = { ',', '!', '?' };
int first = text.IndexOfAny(marks); // 5 (the comma)
int later = text.IndexOfAny(marks, 6); // 12 (the !)
Call
What it does
text.IndexOfAny(marks)
First position of any character in marks.
text.IndexOfAny(marks, 6)
Start looking at index 6.
text.IndexOfAny(marks, 6, 4)
Look at only 4 characters, starting at index 6.
text.IndexOfAny(new[] { 'a', 'e' })
Build the set inline when it is short.
Matching is ordinal: each character is compared by its exact code, so 'A' and 'a' are different. There is no overload with StringComparison. If you need case-insensitive matching, put both cases in the array. Passing null for the array throws ArgumentNullException.
Library declarations
C#
public int IndexOfAny(char[] anyOf);
public int IndexOfAny(char[] anyOf, int startIndex);
public int IndexOfAny(char[] anyOf, int startIndex, int count);
Parameters
Parameter
Description
anyOf
Required. The set of characters to look for. Must not be null.
startIndex
Optional. Where the search begins (0 to Length).
count
Optional. How many characters to examine.
Return Value
Return Value
Description
0 or more
Index of the first character that belongs to anyOf.
-1
None of the characters were found.
C# Examples
Example 1 — Find the First Punctuation Mark
Ask for the first comma, exclamation mark, or question mark in one call.
C#
using System;
class Program {
static void Main() {
string text = "hello, world!";
char[] punct = { ',', '!', '?' };
int pos = text.IndexOfAny(punct);
Console.WriteLine(pos);
Console.WriteLine(text[pos]);
}
}
Output
5
,
How It Works
One set, three choices. The array holds three characters. Any of them counts as a match.
Text order wins. The ! is listed second in the set, but the comma comes first in the text (index 5), so 5 is returned.
Read the matched character.text[pos] reads the character at that index, which shows which member of the set was found.
No question mark needed. The '?' never appears, and that is fine; only one member of the set has to be present.
Example 2 — Find Where the Numbers Start
Use a set of the ten digits to find where the number part of a product code begins, and see the -1 result for text with no digits.
C#
using System;
class Program {
static void Main() {
char[] digits = "0123456789".ToCharArray();
string code = "ABC-2024-x";
int start = code.IndexOfAny(digits);
Console.WriteLine(start);
Console.WriteLine(code.Substring(0, start));
Console.WriteLine("no digits here".IndexOfAny(digits));
}
}
Output
4
ABC-
-1
How It Works
Build the set from a string.ToCharArray() turns "0123456789" into ten separate characters, which saves typing them one by one.
First digit.A, B, C, and - fill indexes 0 to 3, and the 2 at index 4 is the first digit.
Take the prefix.Substring(0, 4) keeps everything before the number, which is ABC-.
Nothing to find. The last phrase has only letters and spaces, so the result is -1. Guard that case before calling Substring.
Example 3 — List Every Vowel Position
Repeat the search with a moving start position to report each vowel in a word.
C#
using System;
class Program {
static void Main() {
string word = "education";
char[] vowels = { 'a', 'e', 'i', 'o', 'u' };
int pos = word.IndexOfAny(vowels);
while (pos != -1) {
Console.WriteLine(word[pos] + " at " + pos);
pos = word.IndexOfAny(vowels, pos + 1);
}
}
}
Output
e at 0
u at 2
a at 4
i at 6
o at 7
How It Works
The word starts with a vowel. The first call returns 0, because e is the very first character.
Continue after each hit. The second argument, pos + 1, skips the vowel just found and searches the rest of the word.
Different vowels, one loop. Each round can match a different member of the set: u, then a, then i and o next to each other.
Stop at -1. After o at 7 only n remains, so the next search returns -1 and the loop ends.
Frequently Asked Questions
It returns the position of the first character in the string that matches any character in the char array you pass. If none match, it returns -1.
No. Each entry in the array is a single character. To find a word or longer text, use IndexOf(string) with StringComparison.Ordinal.
Yes. It compares exact character codes. To match both cases, include both, for example new[] { 'a', 'A' }.
It throws ArgumentNullException. An empty array is allowed and simply returns -1.
IndexOfAny scans from the start and returns the first match. LastIndexOfAny scans from the end and returns the last match.