我正在尝试编写一个简单的程序,该程序将从 2 个文本框中获取 2 个多行输入,将它们放入 2 个数组中并进行比较。
我想检查数组 1 中的条目(文本框 1 的每一行是数组 1 中的单独条目)是否在数组 2 中(文本框 2 的每一行都是数组 2 中的单独条目)。
然后将结果输出到文本框。
例如:
数组 1“一、二、三、四、六”
数组 2“一、三、五、四”
它应该输出:
one = found
two = not found
three = found
four = found
six = not found
我到目前为止的代码如下:
private void button1_Click(object sender, EventArgs e)
{
textBox3.Text = "";
string[] New = textBox1.Text.Split('\n');
string[] Existing = textBox2.Text.Split('\n');
//for each line in textbox1's array
foreach (string str in New)
{
//if the string is present in textbox2's array
if (Existing.Contains(str))
{
textBox3.Text = " ##" + textBox3.Text + str + "found";
}
/if the string is not present in textbox2's array
else
{
textBox3.Text = " ##" +textBox3.Text + str + "not found";
}
}
}
如果任一文本框中有不止一行,这将无法正常工作 - 我无法弄清楚为什么..在测试运行中发生以下情况:
Array 1 - "One"
Array 2 - "One"
Result = One Found
Array 1 - "One"
Array 2 - "One, Two"
Result = One Not Found
Array 1 - "One, Two"
Array 2 - "One, Two"
Result = One found, Two Found
Array 1 - "One, Two, Three"
Array 2 - "One, Two"
Result - One Found, Two Not Found, Three Not Found
提前致谢