比较两个字符串[]

时间:2011-06-04 20:18:39

标签: c# asp.net

在这里给我一个错误:

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";
}

4 个答案:

答案 0 :(得分:6)

怎么样:

 bool areSame = list.SequenceEqual(list1);

答案 1 :(得分:5)

使用Linq的SequenceEqual检查字符串数组是否相同

http://msdn.microsoft.com/en-us/library/bb348567.aspx

答案 2 :(得分:2)

这是因为string.Compare没有接受数组的签名。

此外,当你传递数组时,你不需要在变量名之后使用[]

有一个很棒的SO Question here回答了如何比较两个数组的问题。

答案 3 :(得分:1)

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

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

您需要确定不同长度数组的行为,为同一索引中的不同值返回的内容等等。

相关问题