Case insensitive matching in Java switch-case statement

前端 未结 4 1881
再見小時候
再見小時候 2020-12-01 23:44

I was wondering if there is a way to perform case insensitive match in java switch case statement. the default implementation is case sensitive. Please see

4条回答
  •  臣服心动
    2020-12-02 00:27

    To avoid having to use the case expression to verify if it is lowercase or uppercase, I recommend that you use the following:

    String value = String.valueOf(userChoice).toUpperCase();
    

    This helps to make the conversion of lowercase to uppercase before doing the evaluation in the switch case.

提交回复
热议问题