C# String IndexOf Method

  Keywords : string indexof c#, c# indexof example, c# array indexof example, c# list indexof example, csharp string indexof example, c# indexof example

Here we will learn string indexof method in c# with examples and how to use c# string indexof method to get the index of specified character in the given string with examples.

C# String IndexOf Method

In c#, the string IndexOf method is used to return the index of first occurrence of the specified character in the given string.

 

Generally, in c# the string IndexOf method will start searching for the specified character starting from the Zero position and return the first occurrence of the specified character in the given string.

 

In case, if you want to change the specified character search starting position and to examine only specified number of character positions in string, then we can do it by specifying the required start position and number of character positions to search in string IndexOf method.

 

Following is the pictorial representation of searching for specified character position in the given string using IndexOf() method in c# programming language.

 

C# String IndexOf() Method Representation Diagram

 

If you observe above diagram, we are trying to get the character “s” position in the given stringSuresh Dasari” and it returned the position as 4.

 

In c#, the string IndexOf() method will perform a case-sensitive search to get the specified character position that’s the reason we got the result as 4 even the character “S” exists at the starting position 0.

C# String IndexOf Method Syntax

Following is the syntax of defining a string IndexOf method to get the specified character position in c# programming language.

 

public int IndexOf(char ch)

public int IndexOf(char ch, int startIndex)

public int IndexOf(char ch, int startIndex, int count)

public int IndexOf(char ch, StringComparison comparisonType)

public int IndexOf(string str)

public int IndexOf(string str, int startIndex)

public int IndexOf(string str, int startIndex, int count)

public int IndexOf(string str, StringComparison comparisonType)

If you observe syntaxes, the IndexOf method will return the index position of specified character or a string in the given string and we can change the starting position of characters search and length of characters to search based on our requirements.

 

The string IndexOf method will return an index position of specified character if that character is found. In case, the specified character is not found, then it will return -1.

C# String IndexOf Method Example

Following is the example of using string IndexOf() method to return an index position of specified character in the given string c# programming language.

 

using System;

using System.Collections.Generic;

 

namespace Tutlane

{

    class Program

    {

        static void Main(string[] args)

        {

            string name = "Suresh Dasari";

            Console.WriteLine("Character s Index Position: {0}", name.IndexOf("s"));

            Console.WriteLine("Ignore Case: {0}", name.IndexOf("s", StringComparison.OrdinalIgnoreCase));

            Console.WriteLine("Change Search Start Position: {0}", name.IndexOf("s", 5));

            Console.WriteLine("Characters Length Reult: {0}", name.IndexOf("s", 5, 3));

            Console.WriteLine("String Position: {0}", name.IndexOf("Dasa"));

            Console.WriteLine("\nPress Any Key to Exit..");

            Console.ReadLine();

        }

    }

}

If you observe above example, we used a IndexOf() method to find the index position of defined character in string “Suresh Dasari” with multiple conditions.

 

When we execute above c# program, we will get the result like as shown below.

 

C# String IndexOf() Method Example Result

 

This is how we can use string IndexOf() method to get the position of specified character in given string in c# programming language.