In this article we will explain how to use string IndexOf method or what is string IndexOf method in c# with example.


In c# programming language 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.


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;
namespace SQLTutorialsPoint
    class Program
        static void Main(string[] args)
            string name = "Aditya Kumar Patel";
            Console.WriteLine("Character s Index Position: {0}", name.IndexOf("K"));
            Console.WriteLine("Ignore Case:{0}", name.IndexOf("k", StringComparison.OrdinalIgnoreCase));
            Console.WriteLine("Change Search Start Position: {0}", name.IndexOf("k", 5));
            Console.WriteLine("Characters Length Reult: {0}", name.IndexOf("k", 5,3));
            Console.WriteLine("String Position: {0}", name.IndexOf("Kumar"));
            Console.WriteLine("\nPress Enter Key to Exit..");

As you see above example, we used a IndexOf() method to find the index position of defined character in string “Aditya Kumar Patel” with multiple conditions.

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

 How to use string IndexOf method in c#

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

I hope it will help to you after reading.

Leave a comment

Make sure you enter the (*) required information where indicated. HTML code is not allowed.

You may also like