C# | IndexOfAny() Method

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.



filter_none

edit
close

play_arrow

link
brightness_4
code

// 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");
    }
}

chevron_right


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 paramters 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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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");
    }
}

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


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:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.