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-
  • 218,210
  • 55
  • 464
  • 476
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-
  • 218,210
  • 55
  • 464
  • 476
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
  • 9,156
  • 3
  • 39
  • 53
2

Convert your switch string to lower or upper case beforehand

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