If statements are CaSe-Sensitive, But I don’t want them to be.

Updated on July 26, 2017 in [A] C# .Net
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
9 on July 24, 2017

So I wrote a small if statement that asks the user for their gender, whether it be male, female, or anything else. Currently, if the user types ‘Male’ Capitol M, they get a response tailored to that result. But if they type in ‘male’ lowercase m, they get the result that is suppose to go to the ‘other’ result. Is there a way to make it so that if the user types Male or male they get the correct result? I tried using the || operator but it gives back the error “Operator ‘||’ cannot be assigned to type ‘bool’ or ‘string’ “

 

Console.WriteLine("Now finally " + mainCharacter.name + " what is your gender?");
 Console.Write("Gender: ");
 mainCharacter.gender = Console.ReadLine();
 if (mainCharacter.gender == "Male" || "male")
 {
 Console.WriteLine("Ah well of course you are! You look like you can take on a Racillion Bear by hand!");
 }
 else if (mainCharacter.gender == "Female")
 {
 Console.WriteLine("Of course! Your beauty would surely turn the king's head!");
 }
 else
 {
 Console.WriteLine("Ah well there is a place for everyone even if you are a " + mainCharacter.gender);
 }
  • Liked by
Reply
2 on July 24, 2017

I just remembered I think in Python there was a way to make the string all uppercase or lower case, if there is such a thing in C#, would that work if I changed “Male” to just “male”?

 

on July 25, 2017

Thank you, I will try this!

Show more replies
  • Liked by
Reply
Cancel
1 on July 24, 2017

Use google it’s a beautiful thing!

As for your if statement it just isn’t correct. See it like this:

First the if statement asks “is the gender Male” if this is yes execute my code. 

If this isn’t true don’t execute it.

 

This is a base if statement if you want to check something else in the same you indeed use || but you basically have to ask a different question, the statement doesn’t care for the part before the || if that result returned false.

 

So your if statement should be something like if(this == that || this == somethingelse)

 

As for making a string upper case and lowercase, yes you have that in C# sharp too. But as I said at the start: “Use google it’s a beautiful thing!”

on July 25, 2017

Thank you very much. I did use google to find the || operators  but I had no idea that I had to write the code as this == that || this == something else because in the C# reference page that microsoft had up they only had numbers and not really using strings. But thank you again

Show more replies
  • Liked by
Reply
Cancel
2 on July 26, 2017
string mixedCase = "FeMaLe";
if(mixedCase.ToLower() == @"female") {
  //this is true, do stuff
}

 

Helpful
on July 26, 2017
Show more replies
  • Liked by
Reply
Cancel
0 on July 26, 2017

Actually adding to what Job said. Any single conditional statement must have an l-value and an r-value, where l and r just mean left and right. Your original statement has 2 conditions, with only one l-value and 2 r-values.

In the vein of what you were wanting to do. You can use ToLower() like Leon said. You can also create what are called Extension Methods that add functionality to existing classes in C#. Below I’ll paste in some code where you can add a Has method to the string class that allows you to specific whether or not you want the check to be case sensitive or not and let’s you list any number of arguments to check against and shows an example and the output in Unity via screen snip.

using UnityEngine;
public static class ExtensionMethods {
  public static bool Has(
    this string thisString,
    bool isCaseSensitive,
    params string[] thoseStrings
  ) {
    var leftString = thisString;
    if(!isCaseSensitive)
      leftString = thisString.ToLower();
    foreach (var currentString in thoseStrings) {
      var rightString = currentString;
      if(!isCaseSensitive)
        rightString = currentString.ToLower();
      if (leftString == rightString)
        return true;
    }
    return false;
  }
}

And here is the example usage of it.

var testString = "fEmAlE";
print(
  "testString.Has(true, \"car\", \"man\", \"female\") == " +
  testString.Has(true, "car", "man", "female")
);
print(
  "testString.Has(false, \"car\", \"man\", \"female\") == " +
  testString.Has(false, "car", "man", "female")
);

As you can see in the screen snip below where I tested it in Unity console, the first one I tell it isCaseSensitive is true, so it checks the exact values…which you want sometimes, for instance checking a password. But for times like what you are doing, not. So on the second one you can see I passed false to isCaseSensitive, and the result shows that it evaluated to True even those the case didn’t match, but the characters themselves did.

The extra stuff I threw in there like params string[] thoseStrings, is a way of allowing you to pass as many arguments of a single type as you like to a method and you can access it via an array. This allows you to use it intuitively like what you tried with if(this == “this” || “that”). You can just use the .Has method on any string you have like yourString.Has(false, “this”, “that”). You could remove the bool isCaseSensitive parameter if you wanted to avoid having to specify every time, or make another method called HasCase() or something that is like the one I have for those occasions. In that case you’d have 2 methods that have a quality they omit and no need for the extra parameter.

Anyway, I hope any of that was useful.

  • Liked by
Reply
Cancel