find and solve || findandsolve.com
Please wait.....

C# String.IndexOf( ) Method

In the C#, IndexOf() method is a string method which method used to find the zero based index of the first occurrence of a specified.IndexOf method from the start to the end, the IndexOf method in C# searches one string for another.

Syntax

public int IndexOf(Char ch)  
public int IndexOf(Char, Int32)
public int IndexOf(Char, Int32, Int32)
public int IndexOf(String)
public int IndexOf(String, Int32)
public int IndexOf(String, Int32, Int32)
public int IndexOf(String, Int32, Int32, StringComparision)
public int IndexOf(String, Int32, StringComparision)
public int IndexOf(String, StringComparision)  

Parameters

ch: it is a character type parameter.

Returns

Integer - If the parameter String occurred as a substring in the specified String.

Exceptions

System.ArgumentNullException: If the Argument is null.

public int IndexOf(char ch)

using System;
public class Program
{
 public static void Main()
 {
   string str = "FindAndSolve";
        int index1 = str.IndexOf('S');
        Console.WriteLine("The Index Value of character 'S' is " + index1);
        // Now finding the index of that character which
        // is not even present with the string
        int index2 = str.IndexOf('A');
        // As expected, this will output value -1
        Console.WriteLine("The Index Value of character 'A' is " + index2);
 }
}

Output

The Index Value of character 'S' is 7

The Index Value of character 'A' is 4


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 ch, int intA)

Parameters: This method takes two parameters i.e char ch of type System.Char which specified the character to be searched and intA 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 intA is less than 0 (zero) or greater than the length of the string.

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

using System;
public class Program
{
 public static void Main()
 {
    string str = "FindAndSolve";
         int intA = str.IndexOf('F', 0);
        Console.WriteLine("The Index Value of character 'F' "+
                          "with start index 0 is " + intA);
        int intA1 = str.IndexOf('F', 5);
        // As expected, this will output value -1
        Console.WriteLine("The Index Value of character 'F' is " + intA1);
 }
}

Output

The Index Value of character 'F' with start index 0 is 0

The Index Value of character 'F' is -1


String.IndexOf(char ch, int intA, int intA1) 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 intA till specified position i.e intA1 and if not found it returns -1.

Syntax

public int IndexOf(char ch, int intA, int intA1)

Parameters: This method takes three parameters i.e char ch  of type System.Char which specified the character to be searched, intA of type System.Int32 which specify the starting position in the form of integer value from where the searching to be started and intA1 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 intA or intA1 is negative or intA is greater than the length of the current string or intA1 is greater than the length of current string minus intA.

Example

using System;
public class Program
{
 public static void Main()
 {
  string str = "How to use c sharp";
        int intA = str.IndexOf('h', 2, 14);
        Console.WriteLine("Index Value of 'h' with start"+
                          " Index =2 and end Index = 15 is " + intA);
        int intA1 = str.IndexOf('h', 1, 8);
        Console.WriteLine("Index Value of 'h' with start"+
                          " Index = 1 and end Index = 8 is " + intA1);
 }
}

Output

Index Value of 'h' with start Index =2 and end Index = 15 is 14

Index Value of 'h' with start Index = 1 and end Index = 8 is -1