我一直在努力解决这个问题。运行此代码时,我不断让索引超出范围。
基本上,我拿了一个文本框,将其拆分为一个数组,然后使用数组的每个索引与一个充满字符串的数组进行比较。贴上相关代码,你们能看出我做错了吗?
我已经在错误点附近设置了一个错误。( <----- )
public partial class MainWindow : Window
{
string[] kbsubject = new string[4000];
string[] kbbody = new string[4000];
string[] wordsplit = new string[4000];
int[] hits = new int[4000];
StreamWriter WriteBody = new StreamWriter("kbsubjecttest.txt");
StreamReader readSubject = new StreamReader("kbsubject.txt");
StreamReader readBody = new StreamReader("kbbody.txt");
int IndexHolder = 0, counter = 0, counterSearch = 0, WordsIndex = 0, counterWord=0, ArrayIndex = 0;
string compareBody, compareSubject;
public MainWindow()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
wordsplit = SearchBox.Text.Split(' ');
diagWindow.Items.Add(wordsplit.Length);
diagWindow.Items.Add("Preforming search by split");
WordsIndex = 1;
counterWord = 1;
while (counterSearch != wordsplit.Length)
{
if (kbbody[counterWord].Contains(wordsplit[WordsIndex])) <--------
{
hits[ArrayIndex] = counterWord;
ArrayIndex++;
counterWord++;
WordsIndex++;
}
else
{
ArrayIndex++;
counterWord++;
WordsIndex++;
}
}
}