C# String - IndexOfAny() Method



The C# String IndexOfAny() method is used to retrieve the index of the first occurrence of any character in a specified array of characters within the string.

This method returns -1 if the character in the array is not found within the string.

Syntax

Following are the syntax of the C# string IndexOfAny() method −

public int IndexOfAny(char[] anyOf);
public int IndexOfAny(char[] anyOf, int startIndex);
public int IndexOfAny(char[] anyOf, int startIndex, int count);

Parameters

This method accepts the following parameters −

  • anyOf: It is a required parameter. Which represent an array of characters to search for in a string.
  • startIndex: It is an optional parameter. The starting position in the string to start the search.
  • count: It is an optional parameter. The number of character in the string to search.

Return Value

This method returns a zero-based index position of the first occurrence of any character in the specified array.

Example 1: Default IndexOfAny(char[] anyOf) Method

Following is a basic example of the IndexOfAny() method to retrieve the index of the first occurrence of any character in an array of characters −

using System;
class Program {
   static void Main() {
      string str = "Hii, tutorialspoint!";
      // Array of characters to search for
      char[] charsToFind = { 'i', 'o', '!' };
      int first_indx = str.IndexOfAny(charsToFind);      
      Console.WriteLine(first_indx);
   }
}

Output

Following is the output −

1

Example 2: Find First Character Indices within String

Let's look at another example. Here, we use the IndexOfAny() method to find the index of the first occurrence of any characters from the character array within the given string −

using System;
class Program {
   static void Main() {
      string str = "Hello, World!";
      char[] charsToFind = { 'W', 'o', '!' };
      int index = str.IndexOfAny(charsToFind);
      Console.WriteLine(index);
   }
}

Output

Following is the output −

4

Example 3: IndexOfAny Method with StartIndex

In this example, we use the IndexOfAny() method to find the first index of any character from the given start index from the character array within the string −

using System;
class Program {
   static void Main() {
      string str = "Hello, tutorialspoint";
      char[] charsToFind = { 'W', 'o', 't' };
      
      // Start search after "Hello, "
      int index = str.IndexOfAny(charsToFind, 7); 
      Console.WriteLine(index);
   }
}

Output

Following is the output −

7

Example 4: IndexOfAny() Method with StartIndex and Count

The example below uses the IndexOfAny() method to find the index of the first occurrence of character with the string only for the given count from the given startIndex −

using System;
class Program {
   static void Main() {
      string str = "Hello, tutorialspoint";
      char[] charsToFind = { 'W', 'o', 'n' };
      
      // Search only 5 characters starting from index 7
      int index = str.IndexOfAny(charsToFind, 7, 5);
      Console.WriteLine(index);
   }
}

Output

Following is the output −

10
csharp_strings.htm
Advertisements