C# String IndexOfAny() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

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 !)
CallWhat 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

ParameterDescription
anyOfRequired. The set of characters to look for. Must not be null.
startIndexOptional. Where the search begins (0 to Length).
countOptional. How many characters to examine.

Return Value

Return ValueDescription
0 or moreIndex of the first character that belongs to anyOf.
-1None 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]);
    }
}

How It Works

  1. One set, three choices. The array holds three characters. Any of them counts as a match.
  2. Text order wins. The ! is listed second in the set, but the comma comes first in the text (index 5), so 5 is returned.
  3. Read the matched character. text[pos] reads the character at that index, which shows which member of the set was found.
  4. 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));
    }
}

How It Works

  1. Build the set from a string. ToCharArray() turns "0123456789" into ten separate characters, which saves typing them one by one.
  2. First digit. A, B, C, and - fill indexes 0 to 3, and the 2 at index 4 is the first digit.
  3. Take the prefix. Substring(0, 4) keeps everything before the number, which is ABC-.
  4. 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);
        }
    }
}

How It Works

  1. The word starts with a vowel. The first call returns 0, because e is the very first character.
  2. Continue after each hit. The second argument, pos + 1, skips the vowel just found and searches the rest of the word.
  3. 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.
  4. 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.

Next: Insert()

Put new text into a string at a chosen position and get a new string back.

Insert() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful