Open In App

C# String.IndexOf( ) Method | Set – 1

Last Updated : 12 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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

Output
The 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);
	}
}

Output
The 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);
    }
}

Output
Index 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);
	}
}

Output
First 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.



Article Tags :

Similar Reads