简体   繁体   中英

C#: Confusion about ToUpper() and ToLower()

if I do something like this...

String myVar = "in";
if(myVar.ToUpper() == "in")
{
    //do something
}

This is not going to go inside "if" block ..right?

or

Is it going to check BOTH for "in" AND "IN" and do whatever is there inside that if? If so, why is that ? Isn't it supposed to skip what's inside of "if" block?

Same confusion is about ToLower() too

Edit : So to check for both cases, I need to write:

if((myVar.ToUpper().Equals("in"))&&(myVar.Equals("in")))

Like this..right?

Rather than converting to upper case and then comparing, you should use an equality comparison which can be made case-insensitive. For example:

if (myVar.Equals("in", StringComparison.OrdinalIgnoreCase))
{
     ...
}

You should consider carefully exactly which rules are appropriate - ordinal, the current culture, the invariant culture, or possibly another culture entirely (eg using StringComparer.Create(culture, true) ).

For more details around this, read the MSDN Best Practices for Using Strings in the .NET Framework article.

The expression something.ToUpper().Equals("lowercaseletters") will never be true, so in your example the if-block will not be executed. And of course, this applies to ToLower as well; something.ToLower().Equals("UPPERCASE") will never be true, either.

"IN" is not equal to "in" - so it does not execute the if block. In the case of toLower() it would execute the if block as "in" equals "in"..

First if you want to compare strings use .Equals()

myVar.toUpper().Equals("in")

Second first all code inside the if is executed, only after that the return is tested.

so

String myVar="in";
if(myVar.toUpper().Equals("in"))
{
  //do something
}

don't "do something" .

If you do something like you said, it will not go into the if block, and here is why:

Operators are applied to the object on the left. So your code is the same as writing this:

String myVar="in";
String testVar = myVar.ToUpper();
if(testVar=="in") //This will never be true
{
  //do something
}

In your edit, you still aren't testing if your string is == "IN", you are doing 2 tests to see if your string is == "in".

If you changed your original to this it would work:

String myVar="in";
if(myVar.ToUpper()=="IN")
{
  //do something
}

Your edit should be like this to test both cases:

if((myVar.ToUpper().Equals("IN"))&&(myVar.Equals("in")))

EDIT: Some more explanation from Steven's Comment:

if((myVar.ToUpper().Equals("IN"))&&(myVar.Equals("in")))

That code sample does 2 comparisons, but if myVar will only ever be mixed case versions of in (IE: in In iN IN) then the second comparison is not necessary. Once I have converted the string to ToUpper(), you only need to check if it is equal to IN. So I would replace that line with:

if(myVar.ToUpper().Equals("IN"))

or

if(myVar.ToUpper() == "IN")

I would personally use the == not the .Equals method.

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