How to find substring from string without using indexof method in C#?

前端 未结 7 1424
时光取名叫无心
时光取名叫无心 2020-12-20 05:44

I want to find the position of a substring in a string if present without using any string method including indexof. I tried so much times but failed. Will anybody tell me h

7条回答
  •  伪装坚强ぢ
    2020-12-20 06:21

            string mainString = Console.ReadLine();
            string subString = Console.ReadLine();
            for (int i = 0; i <= mainString.Length - subString.Length; i++)
            {
                bool match = true;
                for (int j = 0; j < subString.Length && mainString[i + j] != subString[j]; j++)
                {
                    match = false;
                }
                if (match)
                    Console.WriteLine(i);
            }    
    

提交回复
热议问题