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.
- IndexOfAny Method (Char[])
- IndexOfAny Method (Char[], Int32)
- IndexOfAny Method (Char[], Int32, Int32)
IndexOfAny Method (Char[])
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)
- Parameter: This method will take a single parameter of type System.Char[] which is the character array containing one or more characters to be searched.
- Return Value: This method will always return the first occurrence in the current instance where any character of ch was found. It used the zero-based index position. The return type of this method is System.Int32.
- Exception: This method can give exception ArgumentNullException if ch is null.
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 Found
IndexOfAny 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)
- Parameters: This method takes two parameters i.e. char[] ch of type System.Char[] which specify the array of characters to be searched and startIndex of type System.Int32 which specify the starting index from where the string to be searched.
- Return Value: This method will always return the first occurrence in the current instance where any character of ch was found. It used the zero-based index position. The return type of this method is System.Int32.
- Exceptions: This method will give two exceptions i.e. ArgumentNullException if ch is null and ArgumentOutOfRangeException if the startindex is greater than the string to be searched or either it is negative.
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 Found
public 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)
- Parameters: This method takes three parameters i.e char[] ch of type System.Char[] which specify the array of characters to be searched, startIndex of type System.Int32 which specify the starting index from where the string to be searched and count of type System.Int32 specifies the number of character positions to be examined.
- Return Value: This method will always return the first occurrence in the current instance where any character of ch was found. It used the zero-based index position. The return type of this method is System.Int32.
- Exceptions: This method will give two exceptions i.e. ArgumentNullException if ch is null and ArgumentOutOfRangeException if the startindex + count is greater than the string to be searched or either both(startindex and count) are negative.
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:
- The search for character array element is case-sensitive.
- The search ranges from startIndex to the end of the string.
- If character array element is an empty array, the method finds a match at the beginning of the string.
References:
Similar Reads
C# String.IndexOf( ) Method | Set - 1
In C#, the IndexOf() method is a String method. Used to find the zero-based index of the first occurrence of a specified character or string within the current instance of the string. The method returns -1 if the character or string is not found. This method can be overloaded by passing different pa
6 min read
Vector indexOf() Method in Java
The java.util.vector.indexOf(Object element) method is used to check and find the occurrence of a particular element in the vector. If the element is present then the index of the first occurrence of the element is returned otherwise -1 is returned if the vector does not contain the element. Syntax:
3 min read
LinkedList indexOf() Method in Java
In Java, the indexOf() method of the LinkedList class is used to find the index of the first occurrence of the specified element in the list. Syntax of LinkedList indexOf() Method public int indexOf(Object o);Parameter: This method takes an object "o" as an argument.Return type: It returns the index
2 min read
JavaScript Array indexOf() Method
The indexOf() method in JavaScript is used to find the position of the first occurrence of a specific value in an array. If the value is not present, it returns -1. This method is handy for quickly determining where a particular item is located within an array.Syntax:array.indexOf(element, start)Par
3 min read
C# | Array.Find() Method
This method is used to search for an element that matches the conditions defined by the specified predicate and returns the first occurrence within the entire Array. Syntax: public static T Find (T[] array, Predicate<T> match); Here, T is the type of element of the array. Parameters: array: It
3 min read
C# | Array.FindAll() Method
This method is used to retrieve all the elements that match the conditions defined by the specified predicate.Syntax: public static T[] FindAll (T[] array, Predicate match); Here, T is the type of element of the array.Parameters: array: It is the one-dimensional, zero-based array to search.match: It
3 min read
C# | Array.FindLast() Method
This method is used to search for an element that matches the conditions defined by the specified predicate and returns the last occurrence within the entire Array. Syntax: public static T FindLast<T> (T[] array, Predicate<T> match); Parameters: array: It is the one-dimensional, zero-bas
3 min read
JavaScript String indexOf() Method
The indexOf() method in JavaScript is used to find the index of the first occurrence of a specified value within a string. The method returns a 0-based index, making it a fundamental JavaScript string manipulation way.The JavaScript indexOf() method helps locate a substring within a string and retur
3 min read
Array.LastIndexOf Method in C# | Set - 1
Array.LastIndexOf method is used to find the last matching element in a one-dimensional array. It starts the search from the last element of an array. It returns the index of the element that contains the specified value. There are 6 methods in the overload list of this method as follows: LastIndexO
8 min read
JavaScript Array lastIndexOf() Method
The JavaScript Array lastIndexOf() Method is used to find the index of the last occurrence of the search element provided as the argument to the function.Syntax: array.lastIndexOf(element, start)Parameters: This method accepts two parameters as mentioned above and described below: element: This para
3 min read