如何在不使用C#中的indexof方法的情况下从字符串中查找子字符串?

我想在不使用任何字符串方法(包括indexof)的情况下找到字符串中子字符串的位置。 我尝试了很多次却失败了。 有人会告诉我怎么做C#? 我们可以使用.Length运算符。

对不起..以为这对我来说很有趣,所以…

扰流板

class Program { static void Main(string[] args) { string str = "abcdefg"; string substr = "cde"; int index = IndexOf(str, substr); Console.WriteLine(index); Console.ReadLine(); } private static int IndexOf(string str, string substr) { bool match; for (int i = 0; i < str.Length - substr.Length + 1; ++i) { match = true; for (int j = 0; j < substr.Length; ++j) { if (str[i + j] != substr[j]) { match = false; break; } } if (match) return i; } return -1; } } 

假设这是家庭作业,我的建议是要记住一个字符串是一个IEnumerable字符。 所以你可以循环遍历字符串中的字符……

由于任何激发这个问题的家庭作业都已经过期,所以这里有一个合理高效的答案。

简单地循环遍历较大的字符串,并循环通过子字符串比较每个字符作为一个去Θ((n-m+1) m)时间,其中m是子字符串的长度, n是找到较小字符串的索引,或者如果没有匹配则较大的减去长度减去较小的长度。

有一些不同的算法可以提供更好的性能,它们在哪种情况下效果最好.Knuth-Morris-Pratt算法需要Θ(m)来设置然后Θ(n)时间才能找到,因为它首先会创建一个表,以便知道未能找到匹配项时可以跳到多远,而总的来说这可以更快地进行搜索。

考虑一下,如果我们正在寻找"ababcd"并且我们首先找到"abab…" (到目前为止可能匹配),如果下一个字符是c我们仍然可能匹配。 如果它是a我们没有匹配,但应跳过两个字符开始寻找从那开始的匹配。 如果它是其他任何东西,我们应该跳过五个字符并继续寻找那里。 准备桌子告诉我们从那时起跳得多远会让事情变得更快:

 public static int IndexOf(string haystack, string needle) { if(haystack == null || needle == null) throw new ArgumentNullException(); if(needle.Length == 0) return 0;//empty strings are everywhere! if(needle.Length == 1)//can't beat just spinning through for it { char c = needle[0]; for(int idx = 0; idx != haystack.Length; ++idx) if(haystack[idx] == c) return idx; return -1; } if (needle.Length == haystack.Length) return needle == haystack ? 0 : -1; if (needle.Length < haystack.Length) { int m = 0; int i = 0; int[] T = KMPTable(needle); while(m + i < haystack.Length) { if(needle[i] == haystack[m + i]) { if(i == needle.Length - 1) return m == haystack.Length ? -1 : m;//match -1 = failure to find conventional in .NET ++i; } else { m = m + i - T[i]; i = T[i] > -1 ? T[i] : 0; } } } return -1; } private static int[] KMPTable(string sought) { int[] table = new int[sought.Length]; int pos = 2; int cnd = 0; table[0] = -1; table[1] = 0; while(pos < table.Length) if(sought[pos - 1] == sought[cnd]) table[pos++] = ++cnd; else if(cnd > 0) cnd = table[cnd]; else table[pos++] = 0; return table; } 

试试这个:

 public static string BetweenOf(string ActualStr, string StrFirst, string StrLast) { return ActualStr.Substring(ActualStr.IndexOf(StrFirst) + StrFirst.Length, (ActualStr.Substring(ActualStr.IndexOf(StrFirst))).IndexOf(StrLast) + StrLast.Length); } 
  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); } 
 public static findindex(String str,String substr) { char a[]=str.toCharArray(); char b[]=substr.toCharArray(); int j=0,t=0; for(int i=0;i 

试试这个:

 internal bool SearchWord(string str, string searchKey) { int j = 0; bool result = false; for (int i = 0; i < str.Length; i++) { if (searchKey[j] == str[i]) { j++; //count++; } else { j = 0; } if (j == searchKey.Length) { result = true; break; } } return result; }