问题
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.
回答1:
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;
}
回答2:
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:
}
回答3:
Convert your switch string to lower or upper case beforehand
switch("KEK".ToLower())
{
case "kek":
CW("hit!");
break;
}
来源:https://stackoverflow.com/questions/5986694/cs-switch-statement-is-case-sensitive-is-there-a-way-to-toggle-it-so-it-becom