4

C#'s switch() statement is case-sensitive. Is there a way to toggle it so it becomes case-insensitive?

==============================

Thanks, But , I don't like these solutions;

Because case conditions will be a variable , and I don't know if they ALL are UPPER or lower.

Ry-
  • 209,133
  • 54
  • 439
  • 449
xiemails
  • 843
  • 3
  • 8
  • 10

3 Answers3

20

Yes - use ToLower() or ToLowerInvariant() on its operands. For example:

switch(month.ToLower()) {
    case "jan":
    case "january": // These all have to be in lowercase
         // Do something
         break;
}
Ry-
  • 209,133
  • 54
  • 439
  • 449
5

You can do something like this

switch(yourStringVariable.ToUpper()){
    case "YOUR_CASE_COND_1":
     // Do your Case1
    break;

    case "YOUR_CASE_COND_2":
    // Do your Case 2
    break;

    default:
}
YetAnotherUser
  • 8,786
  • 3
  • 37
  • 52
2

Convert your switch string to lower or upper case beforehand

switch("KEK".ToLower())
{
 case "kek":
  CW("hit!");
  break;
}
Kasper Holdum
  • 12,421
  • 6
  • 42
  • 74