简体   繁体   中英

Error in C# when trying to compare a value to a string

I am getting an error when trying the following in C#

if (state != 'WI' && state != 'IL')

That statement is giving me an error that states: Error operator != cannot be applied to operands of type 'string' or 'char'

If this is not possible what is a way to accomplish my goal.

Use double quotes for strings:

if (state != "WI" && state != "IL")

Single quotes are useful for single characters:

char c = 'A';
if (c != 'B') ...

EDIT: others have suggested using Equals to compare and I don't fully agree that it should replace an == approach unless you have a reason to use it. Firstly, if state is null then an exception will be thrown from writing state.Equals("WI") . A way around this is to use String.Compare(state, "WI") instead but that no longer returns a bool and will need to be checked against an integer (0 is returned if they are the same):

if (String.Compare(state, "WI") != 0)

Secondly, I would suggest using either Equals or String.Compare if case-sensitivity matters since both provide overloads to handle that issue:

string foo = "Foo";
string otherFoo = "foo";
Console.WriteLine("Equals: {0}", foo.Equals(otherFoo));
Console.WriteLine("Equals case insensitive: {0}", foo.Equals(otherFoo, StringComparison.InvariantCultureIgnoreCase));
Console.WriteLine("Compare: {0}", String.Compare(foo, otherFoo) == 0);
Console.WriteLine("Compare case insensitive: {0}", String.Compare(foo, otherFoo, StringComparison.InvariantCultureIgnoreCase) == 0);

// make foo null
foo = null;
Console.WriteLine("Null Compare: {0}", String.Compare(foo, otherFoo) == 0);
Console.WriteLine("Null Equals: {0}", foo.Equals(otherFoo)); // exception

使用双引号将字符串文字括起来!

You are using single quotes to delimit your strings. These mean char .

Try:

if (state != "WI" && state != "IL")

with double quotes.

While the code you have is perfectly OK, you should consider using the string.Equals method:

if (!state.Equals("WI") && !state.Equals("IL"))

您应该使用“ WI”,也应使用字符串的equals方法作为!state.Equals("WI")

if ( !state.Equals("WI") && !state.Equals("IL") ) {
}

If this is not possible what is a way to accomplish my goal.

RTFEM! * The error message is clearly indicating that one of the operands is being interpreted as a char and not a string . In the case of

state != 'WI'

there are two operands: state and 'WI' . If you've declared state as a string , then clearly it can't be state that is being interpreted as a char . Thus, it must be 'WI' that is being interpreted as a char . And now you ask yourself, "Why is this being interpreted as a char ? How do I declare a string in C# if not enclosing in single-quotes (" ' ")?" So then you Google "declare string C#" and click "I'm Feeling Lucky" and you hit Strings (C# Programming Guide) which tells you to declare a string literal you enclose in double-quotes (" " ").

Thus,

if (state != "WI" && state != "IL")

* This is not meant to be rude so don't take it that way. This answer is very much in the philosophy of "If you teach a man to fish...."

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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