Exclusive Clothing Sale On Flipkart

Get up to 50% off on our latest collection.

Shop Now

Find Index Of Substring in C#

 

Find Index Of Substring in C#

public static void IndexOfSubstring()

        {

            string strval = "abcfirxyz";

            string substr = "fir";

            bool match;

 

            for (int i = 0; i < strval.Length - substr.Length + 1; ++i)

            {

                match = true;

                for (int j = 0; j < substr.Length; ++j)

                {

                    if (strval[i + j] != substr[j])

                    {

                        match = false;

                        break;

                    }

                }

                if (match)

                {

                    Console.WriteLine(i);

                    Console.ReadLine();

                    return;

                }

            }

 

            Console.WriteLine(-1);

            Console.ReadLine();

        }

Output







Share this

Related Posts

Previous
Next Post »

Exclusive Clothing Sale On Flipkart

Get up to 50% off on our latest collection.

Shop Now