Big Star
Big Star

Reputation: 13

Find string in array and return its index

Is there a way to search through a string array and return its index if it is found. Of course I can just iterate through the array and perform the search myself. But I am wondering if there is a more efficient way of doing it. Kotlin has the "contains" method but that returns a boolean. I want the index position instead.

Upvotes: 1

Views: 1242

Answers (1)

r2rek
r2rek

Reputation: 2233

If you need to find specific string's index, then you should use indexOf() method. E.g.

val index = listOf("a","b","c").indexOf("a")

It will return -1 if the string is not found though.

Upvotes: 3

Related Questions