C# String - IndexOf() Method



The C# String IndexOf() method finds the zero-based index of the first occurrence of a specified character or string within the current object of the string. It returns -1 if the character or string is not found.

It's important to note that this method can be overloaded by passing different argument to it.

Syntax

Following is the syntax of the C# string IndexOf() method −

public int IndexOf(char x)

Parameters

This method accepts a parameter char x of type Char which specifies the character to be searched.

Return value

This method returns a 32-bit signed integer representing the index position of a value if that character is found. Otherwise, it returns -1.

Example 1: Using IndexOf(char x) Syntax

Following is the basic example of theIndexOf(char x)method to find the index of character that is present in the string −

using System;
class Program {
   static void Main() {
      string str = "tutorialspoint";
      
      int indx = str.IndexOf('l');
      Console.Write("The Index Value of character 'l' is " + indx);
   }
}

Output

Following is the output −

The Index Value of character 'l' is 7

Example 2: Using IndexOf(char x, int start1) Syntax

Let us look at another example of the IndexOf(char x, int start1) method. Here, we use another version of the syntax to find the index of specified characters starting from the specified index −

using System;
class Program {
   static void Main() {
      string str = "tutorialspoint";
      
      int indx = str.IndexOf('a', 7);
      Console.WriteLine("The Index Value of character 'a' "+ "with start index 7 is " + indx);
   }
}

Output

Following is the output −

The Index Value of character 'a' with start index 7 is -1

Example 3: Using IndexOf(char x, int start1, int start2) Syntax

In this example, we use the IndexOf(char x, int startIndex, int endIndex) method to find the index of a specified character within a specified range, explicitly defining both the starting and ending indices for the search −

using System;
class Program {
   static void Main(string[] args) {   
   	   string str = "Your Life My Rules";
   	   int index1 = str.IndexOf('R', 2, 14);
   	   // Here starting index is < Index value of 'R'
   	   // Also ending index is > Index of 'R'
   	   Console.WriteLine("Index Value of 'R' with start" + " Index = 2 and end Index = 15 is " + index1);
   }
}

Output

Following is the output −

Index Value of 'R' with start Index = 2 and end Index = 15 is 13

Example 4: Using IndexOf(string s1) Syntax

In this example, we use the IndexOf(string s1) method to find the index of first occurrence of a substring within a string. If the substring is not found, the method returns -1 −

using System;
class Program {
   static void Main(string[] args) {
      string str = "Hello tutorialspoint....How are you...";     
      // use the indexOf(string s1) syntax
      int indx = str.IndexOf("tutorialspoint"); 
      Console.WriteLine("First value Index of 'How' is " + indx);
   }
}

Output

Following is the output −

First value Index of 'How' is 6
csharp_strings.htm
Advertisements