
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Arithmetic Operators
- C# - Assignment Operators
- C# - Relational Operators
- C# - Logical Operators
- C# - Bitwise Operators
- C# - Miscellaneous Operators
- C# - Operators Precedence
- C# Conditional Statements
- C# - Decision Making
- C# - If
- C# - If Else
- C# - Nested If
- C# - Switch
- C# - Nested Switch
- C# Control Statements
- C# - Loops
- C# - For Loop
- C# - While Loop
- C# - Do While Loop
- C# - Nested Loops
- C# - Break
- C# - Continue
- C# OOP & Data Handling
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
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