1

I've got this array:

val array : MutableList<String> =  mutableListOf("laptop on floor", "man is thomas")

How do I check if it contains this string: flo? (It does, in the first element)

1 Answer 1

4

Try this code snippet

val array : MutableList<String> =  mutableListOf("laptop on floor", "man is thomas")

val doesContain1 = array.indexOfFirst { it.contains("flo") } >= 0
// or
val doesContain2 = array.find { it.contains("flo") } != null
// or
val doesContain3 = array.any { it.contains("flo") }
Sign up to request clarification or add additional context in comments.

2 Comments

Of these three options, the third one, using any, is definitely the most idiomatic.
@Sam yes, but the first two also return some extra result

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.