gpt4 book ai didi

kotlin - 当条件与忽略案件在 Kotlin

转载 作者:行者123 更新时间:2023-12-02 13:03:25 25 4
gpt4 key购买 nike

我知道我们可以编写带有忽略大小写的if, else if, else if

if (someString.equals("otherString", ignoreCase = true)) {
}
我对此很好奇,如何在不考虑大小写的情况下编写when(在Java中是一个开关)条件。

最佳答案

有两种选择:

  • 将字符串转换为小写(或大写):
    when (someString.toLowerCase()) {
    "otherString1".toLowerCase() -> { /*...*/ }
    "otherString2".toLowerCase() -> { /*...*/ }
    }
  • 直接使用equals方法:
     when {
    someString.equals("otherString1", ignoreCase = true) -> { /*...*/ }
    someString.equals("otherString2", ignoreCase = true) -> { /*...*/ }
    }
  • 关于kotlin - 当条件与忽略案件在 Kotlin ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63377560/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com