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

In C#, IndexOf() method is a string method. This method is used to find the zero based index of the first occurrence of a specified character or string within 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)

String.IndexOf(char x) method

This method returns the zero-based index of the first occurrence of the specified character within the string. In case no such character founds then it returns -1.

Syntax:



public int IndexOf(char x)
  • Parameters: This method takes a parameter char x of type System.Char which specified 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” and as a result, this method returns respective outcome mainly the first occurrence of character ‘F’. Also in the second case, the character ‘C’ was not present so it simply returns -1.

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the 
// String.IndexOf(char x) method
using System;
namespace ConsoleApplication1 {
  
class Geeks {
  
    // Main Method
    static void Main(string[] args)
    {
  
        string str = "GeeksForGeeks";
  
        // Finding the index of character 
        // which is present in string and
        // this will show the value 5
        int index1 = str.IndexOf('F');
          
        Console.WriteLine("The Index Value of character 'F' is " + 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);
    }
}
}

chevron_right


Output:

The Index Value of character 'F' is 5
The Index Value of character 'C' is -1

String.IndexOf(char x, int start1) method

This method returns the zero-based index of the first occurrence of the specified character within the string. However, the searching of that character will start from a specified position and if not found it returns -1.

Syntax:

public int IndexOf(char x, int start1)
  • Parameters: This method takes two parameters i.e char x of type System.Char which specified the character to be searched and start1 of type System.Int32 which specify the starting position in the form of integer value from where the searching to be started.
  • Return Type: The return type of this method is System.Int32.
  • Exception: This method can give ArgumentOutOfRangeException if the start1 is less than 0 (zero) or greater than the length of the string.

Example: In the below code the User wants to know the index of character ‘H’ within the specified string “HelloGeeks” and as a result, this method returns the respective index of character ‘H’. However if start1 is greater than 1 then it is obvious to return -1.

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the 
// String.IndexOf(char x, int start1) method
using System;
namespace ConsoleApplication2{
  
class Geeks {
  
    // Main Method
    static void Main(string[] args)
    {
  
        string str = "HelloGeeks";
  
        // Finding the index of character
        // which is present in string
        // this will show the value 0
        int index1 = str.IndexOf('H', 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);
    }
}
}

chevron_right


Output:

The Index Value of character 'H' with start index 0 is 0
The Index Value of character 'H' is -1

String.IndexOf(char x, int start1, int start2) method

This method returns the zero-based index of the first occurrence of the specified character within the string. However, the searching 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: This method takes three parameters i.e char x of type System.Char which specified the character to be searched, start1 of type System.Int32 which specify the starting position in the form of integer value from where the searching to be started and start2 of type System.Int32 which specify the ending position where searching is to be stopped.
  • Return Type: The return type of this method is System.Int32.
  • Exception: This method can give ArgumentOutOfRangeException if the start1 or start2 is negative or start1 is greater than the length of the current string or start2 is greater than the length of current string minus start1.

Example: In the below code the User wants to know the index of character ‘R’ within the specified string “My Life My Rules” and as a result, this method returns the index value of character ‘R’. Again for the case that start1>1 and start2<8 it again returns -1 because it didn't find any character.

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the
// String.IndexOf(char x, int start1,
//  int start2) method
using System;
namespace ConsoleApplication3 {
  
class Geeks {
  
    // Main Method
    static void Main(string[] args)
    {
  
        string str = "My 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'
        // Hence It is obvious to return 11
        Console.WriteLine("Index Value of 'R' with start"
                          " Index =2 and end Index = 15 is " + index1);
  
        // Now here starting index is chosen right
        // However ending position is < index of 'R'
        // Surely it will return -1
        int index2 = str.IndexOf('R', 1, 8);
  
        Console.WriteLine("Index Value of 'R' with start"+
                          " Index = 1 and end Index = 8 is " + index2);
    }
}
}

chevron_right


Output:

Index Value of 'R' with start Index =2 and end Index = 15 is 11
Index Value of 'R' with start Index = 1 and end Index = 8 is -1

String.IndexOf(string s1) method

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 case of characters.

Syntax:

public int IndexOf(string s1)
  • Parameters: This method takes a parameter s1 of type System.String which specified 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: This method can give ArgumentNullException if the s1 is null.

Example: In the below code, it is known that 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 name “Chair” so, it simply returns -1.

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the
// String.IndexOf(string  s1) method
using System;
namespace ConsoleApplication4 {
  
class Geeks {
  
    // Main Method
    static void Main(string[] args)
    {
  
        string str = "Hello Friends....How are you...";
  
        int i = str.IndexOf("How");
  
        // As this string is present in the 
        // main string then it will obviously
        //  output the value as 17
        Console.WriteLine("First value Index of 'How' is " + i);
  
        // now the following string is not present
        // So as per the rules, it will return -1
        int i1 = str.IndexOf("Chair");
  
        // As this string is present in 
        // the main string then it will 
        // obviously output the value as -1
        Console.WriteLine("First value Index of 'Chair' is " + i1);
    }
}
}

chevron_right


Output:

First value Index of 'How' is 17
First value Index of 'Chair' is -1



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.