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
EmoticonEmoticon