1

I know we can write if, else if, else if with a ignore case.

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

I am very curious about this, how to write a when(in Java it is a switch) condition with ignoring the case.

Sergio
  • 27,326
  • 8
  • 128
  • 149
Shailendra Madda
  • 20,649
  • 15
  • 100
  • 138

1 Answers1

11

There are a couple of options:

  1. Translate strings to lower (or upper) case:

    when (someString.toLowerCase()) {
        "otherString1".toLowerCase() -> { /*...*/ }
        "otherString2".toLowerCase() -> { /*...*/ }
    }
    
  2. Directly use equals method:

     when {
         someString.equals("otherString1", ignoreCase = true) -> { /*...*/ }
         someString.equals("otherString2", ignoreCase = true) -> { /*...*/ }
     }    
    
Sergio
  • 27,326
  • 8
  • 128
  • 149