In C#, the IndexOf()
method is used to find the index of the first occurrence of a specified character or substring within a string. It returns the zero-based index of the first occurrence, or -1 if the character or substring is not found.
There are several overloaded versions of the IndexOf()
method in C#, allowing you to search for different types of characters or substrings. Here are the commonly used versions:
IndexOf(char value)
: This version searches for the specified character within the string.
string str = "Hello, World!"; int index = str.IndexOf('W'); Console.WriteLine(index); // Output: 7
IndexOf(string value)
: This version searches for the specified substring within the string.
string str = "Hello, World!"; int index = str.IndexOf("World"); Console.WriteLine(index); // Output: 7
IndexOf(char value, int startIndex)
: This version starts searching for the specified character from the given starting index.
string str = "Hello, World!"; int index = str.IndexOf('o', 5); Console.WriteLine(index); // Output: 7
IndexOf(string value, int startIndex)
: This version starts searching for the specified substring from the given starting index.
string str = "Hello, World!"; int index = str.IndexOf("o", 5); Console.WriteLine(index); // Output: 7
There are more variations of the IndexOf()
method that allow you to specify additional search parameters, such as ignoring case or specifying a range. You can find the complete list of overloads in the official Microsoft documentation for the String.IndexOf
method.
Signature:
The signature of the IndexOf()
method in C# is as follows:
public int IndexOf(char value)
or
public int IndexOf(string value)
or
public int IndexOf(char value, int startIndex)
or
public int IndexOf(string value, int startIndex)
The IndexOf()
method is a member of the System.String
class in C#. It returns an integer value representing the index of the first occurrence of the specified character or substring within the string. If the character or substring is not found, it returns -1.
Parameters:
The IndexOf()
method in C# can take different parameters based on its overload. Here are the parameters used in various versions of the IndexOf()
method:
IndexOf(char value)
:value
(required): The character to search for within the string.
IndexOf(string value)
:value
(required): The substring to search for within the string.
IndexOf(char value, int startIndex)
:value
(required): The character to search for within the string.startIndex
(optional): The zero-based starting index for the search. If not specified, the search starts from the beginning of the string.
IndexOf(string value, int startIndex)
:value
(required): The substring to search for within the string.startIndex
(optional): The zero-based starting index for the search. If not specified, the search starts from the beginning of the string.
In addition to these parameters, some versions of the IndexOf()
method also accept additional parameters such as StringComparison
or StringComparison.Ordinal
, which allow you to specify the comparison rules for the search.
It’s important to note that the startIndex
parameter is zero-based, meaning the first character has an index of 0. If the startIndex
is out of range or greater than or equal to the length of the string, the method will return -1 to indicate that the character or substring was not found.
Return:
The IndexOf()
method in C# returns an integer value representing the index of the first occurrence of the specified character or substring within the string. Here’s the return value:
- If the character or substring is found within the string, it returns the zero-based index of the first occurrence.
- If the character or substring is not found, it returns -1.
For example:
string str = "Hello, World!"; int index = str.IndexOf('W'); Console.WriteLine(index); // Output: 7
In this example, the IndexOf('W')
method call returns 7 because the character ‘W’ is found at index 7 in the string “Hello, World!”.
If the character or substring is not found, the method returns -1. For example:
string str = "Hello, World!"; int index = str.IndexOf('X'); Console.WriteLine(index); // Output: -1
In this case, the IndexOf('X')
method call returns -1 because the character ‘X’ is not present in the string “Hello, World!”.
You can use the return value of the IndexOf()
method to determine whether a character or substring exists in a string and to retrieve its position for further processing.
C# String IndexOf() Method Example:
Certainly! Here’s an example that demonstrates the usage of the IndexOf()
method in C#:
using System; class Program { static void Main() { string str = "Hello, World!"; // Find the index of a character int charIndex = str.IndexOf('W'); Console.WriteLine("Index of 'W': " + charIndex); // Output: 7 // Find the index of a substring int substringIndex = str.IndexOf("World"); Console.WriteLine("Index of 'World': " + substringIndex); // Output: 7 // Find the index of a character starting from a specific index int charIndexFromIndex = str.IndexOf('o', 5); Console.WriteLine("Index of 'o' from index 5: " + charIndexFromIndex); // Output: 7 // Find the index of a substring starting from a specific index int substringIndexFromIndex = str.IndexOf("o", 5); Console.WriteLine("Index of 'o' from index 5: " + substringIndexFromIndex); // Output: 7 // Find the index of a character that doesn't exist int nonExistingCharIndex = str.IndexOf('X'); Console.WriteLine("Index of 'X': " + nonExistingCharIndex); // Output: -1 // Find the index of a substring that doesn't exist int nonExistingSubstringIndex = str.IndexOf("Goodbye"); Console.WriteLine("Index of 'Goodbye': " + nonExistingSubstringIndex); // Output: -1 } }
In this example, we have a string str
initialized with the value “Hello, World!”. We use the IndexOf()
method to find the index of a character (‘W’), a substring (“World”), and a character or substring starting from a specific index. We also demonstrate how the method returns -1 when the character or substring doesn’t exist in the string.
When you run the program, you will see the corresponding output for each IndexOf()
method call.