Making a textbox comparison case-insensitive

时间:2017-04-10 00:32:19

标签: c# string-comparison case-insensitive

Beginner here. I currently have a comparison with a textbox and a listbox, but I need the comparison to be case-insensitive. I keep seeing StringComparison.CurrentCultureIgnoreCase but am unable to fit it into this line of code where I thought it belonged:

if (listBox1.Items[q].Equals(textBoxYourAnswer.Text))
{
    //stuff
}

Does it go here? Does it only work if the comparison text is in quotes, or will it work when the system is looking at a listBox?

1 个答案:

答案 0 :(得分:1)

The listbox item is of type object, but the Equals overload that accepts a StringComparison argument is specific to the String type. If your listbox item is actually of type string, cast it to a string before calling Equals.

if(((string)listBox1.Items[q]).Equals(textBoxYourAnswer.Text, StringComparison.CurrentCultureIgnoreCase)