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#
using System;
class GFG {
public static void Main()
{
String str = "GeeksForGeeks" ;
Console.WriteLine( "Given String : {0}\n" , str);
char [] ch = { 's' };
Console.WriteLine(str.IndexOfAny(ch) + 1);
char [] ch1 = { 'a' , 'b' , 'c' , 'e' , 'f' };
Console.WriteLine(str.IndexOfAny(ch1) + 1);
char [] ch2 = { 'a' , 'b' , 'c' };
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#
using System;
class GFG {
public static void Main()
{
String str = "GeeksForGeeks" ;
Console.WriteLine( "Given String : {0}\n" , str);
char [] ch = { 's' };
int startIndex = 6;
Console.WriteLine(str.IndexOfAny(ch, startIndex) + 1);
char [] ch1 = { 'a' , 'b' , 'c' , 'e' , 'f' };
int startIndex1 = 4;
Console.WriteLine(str.IndexOfAny(ch1, startIndex1) + 1);
char [] ch2 = { 'a' , 'b' , 'c' , 'f' };
int startIndex2 = 5;
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#
using System;
class GFG {
public static void Main()
{
String str = "GeeksForGeeks" ;
Console.WriteLine( "Given String : {0}\n" , str);
char [] ch = { 's' };
int startIndex = 6;
int count = 4;
int r = str.IndexOfAny(ch, startIndex, count) == -1
? -1 : str.IndexOfAny(ch, startIndex, count) + 1;
Console.WriteLine(r);
char [] ch1 = { 'a' , 'b' , 'c' , 'e' , 'f' };
int startIndex1 = 3;
int count1 = 8;
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;
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
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
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
Array.LastIndexOf Method in C# | Set â 2
Array.LastIndexOf method is used to find the index of the last occurrence of a value in a one-dimensional Array or in a portion of the 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 over
4 min read
C# | Array.LastIndexOf<T>(T[], T) Method
Array.LastIndexOf<T>(T[], T) Method is used to search for the specified object. It returns the index of the last occurrence within the entire Array. Syntax: public static int LastIndexOf<T>(T[] array, T value); Parameters: array: It is a one-dimensional, zero-indexed array to search. val
2 min read
List FindLastIndex() Method in C# | Set -2
This method is used to search for an element which matches the conditions defined by a specified predicate and returns the zero-based index of the last occurrence within the List<T> or a portion of it. There are 3 methods in the overload list of this method: FindLastIndex(Predicate<T>) M
5 min read
List FindLastIndex() Method in C# | Set -1
This method is used to search for an element which matches the conditions defined by a specified predicate and returns the zero-based index of the last occurrence within the List<T> or a portion of it. There are 3 methods in the overload list of this method: FindLastIndex(Predicate<T>) M
3 min read
List.FindIndex() Method in C# with Examples
List<T>.FindIndex Method is used to search for an element that matches the conditions defined by a specified predicate and returns the index of the first occurrence within the List<T>. If an item that matches the conditions is not found then this method will return -1. There are 3 method
6 min read