【问题标题】:when condition with ignore case in koltin当在 kotlin 中忽略大小写的条件
【发布时间】:2020-12-02 06:19:24
【问题描述】:

我知道我们可以用忽略大小写写if, else if, else if

if (someString.equals("otherString", ignoreCase = true)) {
}

对此我很好奇,如何在忽略大小写的情况下编写 when(在 Java 中是 switch)条件。

【问题讨论】:

  • when(someString.toLower()) { "bla" -> ...

标签: kotlin case kotlin-when


【解决方案1】:

有几个选项:

  1. 将字符串转换为小写(或大写):

    when (someString.toLowerCase()) {
        "otherString1".toLowerCase() -> { /*...*/ }
        "otherString2".toLowerCase() -> { /*...*/ }
    }
    
  2. 直接使用equals方法:

     when {
         someString.equals("otherString1", ignoreCase = true) -> { /*...*/ }
         someString.equals("otherString2", ignoreCase = true) -> { /*...*/ }
     }    
    

【讨论】:

  • 感谢您的回答
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-06
  • 2015-01-15
  • 1970-01-01
相关资源
最近更新 更多