2011-06-04 181 views
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 2011-06-04 20:21:01

+1

@ Matt Ball,thecoop,Chris,Alex Aza,Oded:哇,所以你们决定关闭?这个问题有什么含糊或模糊的地方? – 2011-06-04 20:53:38

回答

1

string.Compare没有重载需要字符串数组。

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

您需要决定的行为不同长度的阵列,如何在相同指数等返回不同的值...

2

这是因为string.Compare没有接受阵列的签名。

而且,当你路过你身边的阵列不需要使用[]变量名后,你所做的一切。

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

6

什么:

bool areSame = list.SequenceEqual(list1);