The C # switch statement is case sensitive. Is there a way to switch it so that it becomes case insensitive?

The C # switch () operator is case sensitive. Is there a way to switch it so that it is not case sensitive?

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

Thanks, But I do not like these decisions;

Because the conditions of the condition will be variable, and I do not know if they are ALL UPPER or lower.

+4
source share
3 answers

Yes - use ToLower() or ToLowerInvariant() in your operands. For instance:

 switch(month.ToLower()) { case "jan": case "january": // These all have to be in lowercase // Do something break; } 
+18
source

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: } 
+5
source

Convert the switch string to lower or upper case in advance

 switch("KEK".ToLower()) { case "kek": CW("hit!"); break; } 
+2
source

All Articles