C# String.IndexOf( ) Method | Set – 1
Last Updated :
12 Mar, 2025
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 parameters to it.
- String.IndexOf(char x)
- String.IndexOf(char x, int start1)
- String.IndexOf(char x, int start1, int start2)
- String.IndexOf(string s1)
- String.IndexOf(string s1, int start1)
- String.IndexOf(string s1, int start1, int start2)
- String.IndexOf(string s1, int start1, int start2, StringComparison cType)
- String.IndexOf(string s1, int start1, StringComparison cType)
- String.IndexOf(string s1, StringComparison cType)
1. String.IndexOf(char x)
This method returns the zero-based index of the first occurrence of the specified character within the string. In case no such character is found then it returns -1.
Syntax:
public int IndexOf(char x)
- Parameters: This method takes a parameter char x of type System.Char, which specifies the character to be searched.
- Return Type: The return type of this method is System.Int32
Example: In the below code, the User wants to know the index of character ‘F’ within the specified string “GeeksForGeeks”
C#
// C# program to illustrate the
// String.IndexOf(char x) method
using System;
class Geeks
{
static void Main(string[] args)
{
string str = "GeeksForGeeks";
// Finding the index of character
// which is present in string and
int index1 = str.IndexOf('F');
// Return the the value 5
Console.WriteLine("The Index Value of character 'F': " + index1);
// Now finding the index of that character which
// is not even present with the string
int index2 = str.IndexOf('C');
// As expected, this will output value -1
Console.WriteLine("The Index Value of character 'C' is " + index2);
}
}
OutputThe Index Value of character 'F': 5
The Index Value of character 'C' is -1
2. String.IndexOf(char x, int start1)
This method returns the zero-based index of the first occurrence of the specified character within the string. However, the search for that character will start from a specified position and if not found it returns -1.
Syntax:
public int IndexOf(char x, int start1)
- Parameters: Takes two parameters i.e. char x of type System.Char which specifies the character to be searched and start1 of type System.Int32 specifies the starting position, from where the search is to be started.
- Return Type: The return type of this method is System.Int32.
- Exception: Throw ArgumentOutOfRangeException if the start1 is less than 0 (zero) or greater than the length of the string.
Example: This example aims to find the index of character ‘H’ in the string “HelloGeeks”.
C#
// C# program to illustrate the
// String.IndexOf(char x, int start1) method
using System;
class Geeks
{
static void Main(string[] args)
{
string str = "HelloGeeks";
// Finding the index of character
// which is present in string
int index1 = str.IndexOf('H', 0);
// this will show the value 0
Console.WriteLine("The Index Value of character 'H' "+
"with start index 0 is " + index1);
// Now finding the index of character
// 'H' with starting position greater
// than index position of 'H'
int index2 = str.IndexOf('H', 5);
// As expected, this will output value -1
Console.WriteLine("The Index Value of character 'H' is " + index2);
}
}
OutputThe Index Value of character 'H' with start index 0 is 0
The Index Value of character 'H' is -1
2. String.IndexOf(char x, int start1, int start2)
This method returns the zero-based index of the first occurrence of the specified character within the string. However, the search of that character will start from a specified position start1 till specified position i.e start2 and if not found it returns -1.
Syntax:
public int IndexOf(char x, int start1, int start2)
- Parameters: Takes three parameters i.e. char x of type System.Char which specifies the character to be searched, start1 of type System.Int32 which specifies the starting position in the form of integer value from where the searching is to be started and start2 of type System.Int32 which specifies the ending position where searching is to be stopped.
- Return Type: The return type of this method is System.Int32.
- Exception: Throw ArgumentOutOfRangeException if the start1 or start2 is negative start1 is greater than the length of the current string or start2 is greater than the length of the current string minus start1.
Example: This example demonstrates how to find the character within a string using string indexOf(char x, int start1, int start2).
C#
// C# program to illustrate the
// String.IndexOf(char x, int start1,
// int start2) method
using System;
class Geeks
{
static void Main(string[] args)
{
string str = "Hello Geek for Geeks";
int index1 = str.IndexOf('f', 2, 14);
// Here starting index is < Index value of 'f'
// Also ending index is > Index of 'R'
Console.WriteLine("Index Value of 'f' with start" +
" Index = 2 and end Index = 14 is " + index1);
// It return 11
// Now here starting index is chosen right
// However ending position is < index of 'f'
// Surely it will return -1
int index2 = str.IndexOf('f', 1, 5);
Console.WriteLine("Index Value of 'f' with start" +
" Index = 1 and end Index = 8 is " + index2);
}
}
OutputIndex Value of 'f' with start Index = 2 and end Index = 14 is 11
Index Value of 'f' with start Index = 1 and end Index = 8 is -1
Explanation: In the above example, we want to know the index of character ‘f’ within the specified string “Hello Geeks for Geeks” and as a result, this method returns the index value of character ‘f’. Again for the case where start1 > 1 and start2 < 8. Then it returns -1 because it didn’t find any character.
4. String.IndexOf(string s1)
This method returns the zero-based index of the first occurrence of the specified sub-string within the string. In case no such string is found then it returns -1 same as in the case of characters.
Syntax:
public int IndexOf(string s1)
- Parameters: This method takes a parameter s1 of type System.String specifies the substring to be searched.
- Return Type: The return type of this method is System.Int32. The zero-based index position of s1 if that string is found, or -1 if it is not. If s1 is String.Empty, the return value is 0.
- Exception: It throws an exception ArgumentNullException if the s1 is null.
Example: This example demonstrates the use of String.IndexOf(string s1) to find the index of a substring in a specified string.
C#
// C# program to illustrate the
// String.IndexOf(string s1) method
using System;
class Geeks
{
static void Main(string[] args)
{
string str = "Hello Friends....How are you...";
// It returns the index of the first
// Occurrence of the specified string
int i = str.IndexOf("How");
Console.WriteLine("First value Index of 'How' is " + i);
// now the following string is not present
// it will return -1
int i1 = str.IndexOf("Chair");
Console.WriteLine("First value Index of 'Chair' is " + i1);
}
}
OutputFirst value Index of 'How' is 17
First value Index of 'Chair' is -1
Explanation: In the above example, the string ‘How’ is present in the main string, so it will simply return the index value of its first character. However, in the next case, there is no substring with the name “Chair” so, it simply returns -1.
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# String Insert() Method
The Insert() method in C# is a part of the String class. It is used to insert a new string at a specified index position and return a new string with the inserted string value. This method does not make the changes in the original string because strings are immutable in C#. It returns a new modified
3 min read
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. IndexOfAn
7 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
C# | Char.IsSurrogate(String, Int32) Method
This method is used to indicates whether the character at the specified position in a specified string has a surrogate code unit or not. Syntax: public static bool IsSurrogate (string s, int index); Parameters: s: It is a String. index: It is the character position to evaluate in s. Return Value: Th
4 min read
C# | Char.IsLowSurrogate(String, Int32) Method
This method is used to indicates whether the Char object at the specified position in a string is a low surrogate or not. Syntax: public static bool IsLowSurrogate (string s, int index); Parameters: s: It is a String. index: It is the character position to evaluate in s. Return Value: This method re
4 min read
C# | Get or Set at specified index in StringCollection
StringCollection class is a new addition to the .NET Framework class library that represents a collection of strings. StringCollection class is defined in the System.Collections.Specialized namespace. Properties: StringCollection accepts null as a valid value and allows duplicate elements. String co
2 min read
C# | CharEnumerator.Reset() Method
CharEnumerator.Reset Method is used to initializes the index to a position logically before the first character of the enumerated string. Syntax: public void Reset (); Below are the programs to illustrate the use of CharEnumerator.Reset() Method: Example 1: // C# program to illustrate the // use of
2 min read
C# String Properties
In C#, a String is an array of characters. The string class represents the text as a series of Unicode characters. It provides various properties and methods so that it becomes easy to work with strings. There are two properties in the string class: Chars[Int32]: Used to get the Char object at a spe
4 min read
C# | Insert at the specified index in StringCollection
StringCollection class is a new addition to the .NET Framework class library that represents a collection of strings. StringCollection class is defined in the System.Collections.Specialized namespace. StringCollection.Insert(Int32, String) method is used to insert a string into the StringCollection
2 min read