Finding the Index Order of a Character in a String in C#

In C#, we use the IndexOf() method to find the index order of a character in a string.

The IndexOf() method has 9 different uses. Here, the most used form is shown to you.

In the example below, let's find the index sequence of the "." character in the string yazilimders.com.

namespace ConsoleApplicationTest
{
    class Program
    {
        static void Main(string[] args)
        {

            string str = "yazilimders.com";
            
            int stringIndex = str.IndexOf('.');

            Console.WriteLine(stringIndex);
            Console.ReadLine();

        }
    }
}

Since the "." character is in the 12th row and indexes start from 0 , the value of 11 is assigned to the variable stringIndex and the result is returned to us as 11.

If the specified character is not in the string, the result will return as -1.

Let's examine the code below.

namespace ConsoleApplicationTest
{
    class Program
    {
        static void Main(string[] args)
        {

            string str = "yazilimders.com";
            
            int stringIndex = str.IndexOf('j');

            Console.WriteLine(stringIndex);
            Console.ReadLine();

        }
    }
}

Since the "j" character is not a string, the value of -1 is assigned to the variable and the result is returned to us like this.



You May Interest

C# Reverse Array

C# Finding Tangent of an Angle

C# Linq Contains Method

C# Finding the Sine of an Angle

How to Find the Name of the Operating System in C# ?