I have:
return if(mValue== null || mValue.asString.isNullOrEmpty()) "Home" else mValue.asString
I tried to perform Elvis operator:
return mValue?.asString ?: "Home"
But I also want to check for empty case for mValue as well.
So if mValue is either null or if it's empty, I want to return "Home" else return its content in String, without using when or if.
Thank you!
You cannot actually use elvis operator for the case if the String is empty. According to documentation elvis operator is only used to check whether a variable is null or not. Therefore, I would suggest you using:
return if (mValue.isNullOrEmpty()) "Home" else mValue
Hope it helps!
You could combine orEmpty and ifEmpty:
return mValue?.asString.orEmpty().ifEmpty { "Home" }
Whether that's an improvment or not, I don't know.
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