比较两个字符串数组

3

这里出现了一个错误:

string.Compare(list[], list1[],true); <<<<<<

导致错误发生。
string[] list = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "v", "z" };
string[] list1 = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "v", "z" };

int result = string.Compare(list[], list1[], true);

if (result == 0)
{
    Label1.Text += "Two strings are equal";
}
else if (result == 1)
{
    Label1.Text += "Test String1 is greater than Test String2";
}
else if (result == -1)
{
    Label1.Text += "Test String1 is less than Test String2";
}

2
请阅读此内容:http://tinyurl.com/so-hints - Oded
1
@Matt Ball、thecoop、Chris、Alex Aza、Oded:哇,你们决定关闭了吗?这个问题有什么含糊不清的地方吗? - Nick Kahn
4个回答

6

关于什么:

 bool areSame = list.SequenceEqual(list1);

5

2
这是因为 string.Compare 没有接受数组的签名。当你传递数组时,也不需要像你所做的那样在变量名称后面使用 []。这里有一个很好的SO问题回答了如何比较两个数组的问题。

1

string.Compare没有接受字符串数组的重载。

您需要编写自己的函数来比较这些数组。

您需要决定不同长度的数组的行为,以及在相同索引中具有不同值时返回什么等等...


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接