views:

266

answers:

6

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?

+1  A: 

"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"..

halfdan
+2  A: 

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.

Arve Systad
+1  A: 

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".

fampinheiro
Ok, I'll bite: why use `Equals`?
Steven Sudit
Steven: fampinheiro is writing in Java apparently, where `Equals` is required because `==` is not overloaded for strings.
Gabe
In some languages the “==” operator compares two object references to see whether they refer to the same instance. I do not believe this is the case in c# though; the operator calls String::Equals under the hood. It still compares the byte-by-byte values though, so you in fact probably want .Compare()
RJFalconer
@rjfalconner: ty, i have learned one new thing today =)
fampinheiro
A: 

Do something like

If(string.compare(myvar,"in",true)==0) {

// your code

}

This code will not check for case sensitivity.

saurabh
Unfortunately for your example, C# itself is case-sensitive.
Steven Sudit
+17  A: 

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 (e.g. using StringComparer.Create(culture, true)).

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

Jon Skeet
+1 for being (so far) the only answer that mentions "culture" besides lower-case and upper-case.
stakx
@Jon For if statements I totally agree with you but what about switch statement of strings? I've read somewhere that its best to convert string to upper case e.g. switch(myVar.ToUpper()) { case "MYCASE1": ... }
Bear Monkey
@mjf196: That depends on the context - what's being upper-cased here? If it's user input, you really need to think about exactly what you want the behaviour to be.
Jon Skeet
+1  A: 

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.

thorkia
I don't see the point of doing both tests. If the second is true, the first will also be. Likewise, I don't know why you're using `Equals`. Do you?
Steven Sudit
@Steven I'm using it because that's what the OP used in his questions. In the second situation, you are correct. Just using it becase that is what the OP used.
thorkia
Well, that explains it, but I assure you that our answers are not obligated to repeat any errors from the questions. :-)
Steven Sudit
I know... I am putting an edit on it.
thorkia