Last Updated : 11 Jul, 2025
In C#, IndexOfAny() method is a String Method. It is used to search the specified character index present in the string which will be the first occurrence from start index. It returns index as an integer value. This method can be overloaded by changing the number of arguments passed to it.
This method will take a single parameter which is the character array containing one or more characters to search. It will search from the start index i.e. from 0 by default and returns -1 if no character in ch was found.
Syntax:
public int IndexOfAny(char[] ch)
Example: Program to demonstrate the public int IndexOfAny(char[] ch) method. The IndexOfAny method will search characters from the start to end of the string and return integer value as position which is the first occurrence of that character.
C#
// C# program to illustrate the
// public int IndexOfAny(char[] ch)
using System;
class GFG {
// Main Method
public static void Main()
{
String str = "GeeksForGeeks";
Console.WriteLine("Given String : {0}\n", str);
// to find single character
// to start search to match character
// 's' will be found at 5 position
char[] ch = { 's' };
Console.WriteLine(str.IndexOfAny(ch) + 1);
// to find any character
// to start search to match characters
char[] ch1 = { 'a', 'b', 'c', 'e', 'f' };
// 'a', 'b', 'c', 'e', 'f' then first
// 'e' will be found at 2 position
Console.WriteLine(str.IndexOfAny(ch1) + 1);
char[] ch2 = { 'a', 'b', 'c' };
// if no character found in string
// then return -1;
int m = str.IndexOfAny(ch2);
if (m > -1)
Console.Write(m);
else
Console.WriteLine("Not Found");
}
}
Output:
Given String : GeeksForGeeks 5 2 Not FoundIndexOfAny Method (Char[], Int32)
This method uses the zero-based index of the first occurrence in the current instance of any character in a specified array of characters. The search starts from a specified character position.
Syntax:
public int IndexOfAny(char[] ch, int startIndex)
Example: Program to demonstrate the public int IndexOfAny(char[] ch, int startIndex) method. The IndexOfAny method will search characters from the start to specified startIndex to end of the string and return integer value as position which is the first occurrence of that character.
C#
// C# program to illustrate the
// public int IndexOfAny(char[] ch, int startIndex)
using System;
class GFG {
// Main Method
public static void Main()
{
String str = "GeeksForGeeks";
Console.WriteLine("Given String : {0}\n", str);
// to find single character
char[] ch = { 's' };
int startIndex = 6;
// to start search from index 6 so character
// 's' will be found at 13 position
Console.WriteLine(str.IndexOfAny(ch, startIndex) + 1);
// to find any character
char[] ch1 = {'a', 'b', 'c', 'e', 'f'};
int startIndex1 = 4;
// to start search from index 4 so character
// 'e' will be match first at 10 position
Console.WriteLine(str.IndexOfAny(ch1, startIndex1) + 1);
char[] ch2 = { 'a', 'b', 'c', 'f' };
int startIndex2 = 5;
// to start search from index 5
// so no character found
// its case sensitive search so 'f' is not match
int m = str.IndexOfAny(ch2, startIndex2);
if (m > -1)
Console.Write(m);
else
Console.Write("Not Found");
}
}
Output:
Given String : GeeksForGeeks 13 10 Not Foundpublic int IndexOfAny( char[] ch, int startIndex, int count)
This method uses the zero-based index of the first occurrence in the current instance of any character in a specified array of characters. The search starts from the specified character position and examines a specified number of character positions.
Syntax:
public int IndexOfAny(char[] ch, int startIndex, int count)
Example : Program to demonstrate the public int IndexOfAny( char[] ch, int startIndex, int count) method. This IndexOfAny method will search the characters from specified index to specified index + (count – 1) of the string where count is the number of the characters to examine and then return integer value as position which is the first occurrence of that character.
C#
// C# program to illustrate the
// public int IndexOfAny( char[] ch,
// int startIndex, int count)
using System;
class GFG {
// Main Method
public static void Main()
{
String str = "GeeksForGeeks";
Console.WriteLine("Given String : {0}\n", str);
// to find single character
char[] ch = { 's' };
int startIndex = 6;
int count = 4;
// to start search from index 6
// to 10(6+4)so character
// 's' is not found so return -1
int r = str.IndexOfAny(ch, startIndex, count) == -1
? -1 : str.IndexOfAny(ch, startIndex, count) + 1;
Console.WriteLine(r);
// to find any character
char[] ch1 = {'a', 'b', 'c', 'e', 'f'};
int startIndex1 = 3;
int count1 = 8;
// to start search from index 3
// to 11(3 + 8)so character
// 's' will be match first at 10 position
int r1 = str.IndexOfAny(ch1, startIndex1, count1) == -1
? -1 : str.IndexOfAny(ch1, startIndex1, count1) + 1;
Console.WriteLine(r1);
char[] ch2 = {'a', 'b', 'c', 'F'};
int startIndex2 = 5;
int count2 = 5;
// to start search from index 5
// to 10(5 + 5)so character
// 'F' will be match first at 6 position
int r2 = str.IndexOfAny(ch2, startIndex2, count2) == -1
? -1 : str.IndexOfAny(ch2, startIndex2, count2) + 1;
Console.WriteLine(r2);
}
}
Output:
Given String : GeeksForGeeks -1 10 6
Important Points to Remember:
References:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4