简体   繁体   English

为什么这个比较答案永远不变?

[英]Why does this compare answer never change?

I'm writing code that reads a CSV into an array. 我正在编写将CSV读入数组的代码。 The 7th item in the CSV is TenNinetyNine or W2 so I compare array[6] and TenNinetyNine and it always gives me 1 back saying they match. CSV中的第七项是TenNinetyNineW2所以我比较array[6]TenNinetyNine ,它总是给我1表示它们匹配。 I pull up a messagebox with array[6] and it shows that they don't match. 我用array[6]拉出一个消息框,它表明它们不匹配。

I have tried: if (array[6] == "TenNinetyNine") and using string.Compare which is currently up. 我已经尝试过: if (array[6] == "TenNinetyNine")并使用string.Compare当前正在运行。

I messed with the CSV which was originally 1099 or W2 just to verify they both read in as strings. 我弄混了最初是1099或W2的CSV,只是为了验证它们都是作为字符串读入的。 All my testing shows that every line I see a new value for array[6] but it seems to keep the first result when they are compared. 我所有的测试表明,每一行我都为array[6]看到了一个新值,但是当它们进行比较时,似乎保留了第一个结果。

string data = sr.ReadLine();
while (data != null)
{
    string[] developerData = data.Split(',');
    string tax1 = "TenNinetyNine";
    int taxCompared = string.Compare(tax1, developerData[6]);

    MessageBox.Show(developerData[6]); //Changes each iteration
    MessageBox.Show(taxCompared.ToString()); //Always 1
}

I expect the MessageBox.Show(taxCompared.ToString()); 我期望MessageBox.Show(taxCompared.ToString()); to produce a 1 for TenNinetyNine and a -1 (or 0) for W2 . TenNinetyNine生成1,为W2生成-1(或0)。

Put a breakpoint on the first messagebox so that you can hove over and see tax1 and developerData[6] are identical as I have no idea what developerData[6] is returning. 在第一个消息框上放置一个断点,以便您可以浏览并看到tax1developerData [6]是相同的,因为我不知道将返回什么developerData [6]。 String.Compare is case sensitive so check for that too, ensure there are no leading or trailling spaces by trimming each string: String.Compare区分大小写,因此也要进行检查,并通过修剪每个字符串来确保没有前导或尾随空格:

int taxCompared = string.Compare(tax1.Trim(), developerData[6].Trim();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM