C# as below:
public static int StrStr(string str1, string str2)
{
int result = -1;
int i = 0;
int j = 0;
while (i <= str1.Length - str2.Length)
{
if (str1[i] == str2[j])
{
if (j == str2.Length - 1)
{
result = i - j;
break;
}
else
{
j++;
}
}
else
{
i -= j;
j = 0;
}
i++;
}
return result;
}
本文介绍了一个使用C#实现的简单字符串查找算法。该算法通过遍历目标字符串来寻找是否包含指定子串,并返回首次出现的位置。代码示例展示了如何进行逐字符比较,以及在匹配失败时如何回退并继续搜索。
136

被折叠的 条评论
为什么被折叠?



