https://kotlinlang.org logo
Title
m

Mohamed Ibrahim

07/08/2019, 9:16 AM
is there anyway to use
when
with ignorecase when it comes to comparing strings
d

diesieben07

07/08/2019, 9:18 AM
t

tseisel

07/08/2019, 9:20 AM
How about converting to either uppercase or lowercase in
when
?
when (text.toUpperCase()) {
   "FOO" -> doFoo()
   "BAR" -> doBar()
}
d

diesieben07

07/08/2019, 9:21 AM
That produces an intermediary string though.
m

Mohamed Ibrahim

07/08/2019, 9:25 AM
yeah, but I think we need to do that for both strings
t

tseisel

07/08/2019, 9:58 AM
If your scenario is performance-critical, you may prefer @diesieben07's answer as it does not allocate strings. In any other case, choose whichever you find the most readable.
d

Dominaezzz

07/08/2019, 1:50 PM
Depends on the number of cases. It's pretty much switch (+allocation) vs if else.