In my code I have this if statement:
if (categoryName == "SomeName1" || categoryName == "SomeName2" ||
categoryName == "SomeName3" || categoryName == "SomeName4" ||
categoryName == "SomeName5" || categoryName == "SomeName6") {
// Do something
}
I was wondering if I could shorten this if. Something like:
if (categoryName == "SomeName1" and "SomeName2" and "SomeName3" ...) {
// Do something
}
Is there any aproach to do something like this in Kotlin?
You can use a when statement. The syntax kind of looks like what you were asking for:
when (categoryName) {
"SomeName1", "SomeName2", "SomeName3", "SomeName4", "SomeName5", "SomeName6" -> // Do something
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With