This might be simply a duplicate of Use of Boolean? in if expression, but I don't know enough Kotlin to know whether it is...
I want to test whether body : String?
contains expected : String
as a substring. (If body
is null
, then it doesn't contain the substring.) I've figured out that I can write my condition like this:
if (body?.contains(expected) == true) {
succeed()
}
or like this:
if (body?.contains(expected) != true) {
fail()
}
(where body?.contains(expected)
is an expression of type Boolean?
), but is that really the best or most idiomatic way of writing such a condition? Are there any alternatives (possibly using other member functions of String
) that are easier on the eyes?