How to check if an array of strings contains a specific value in Kotlin?
Hello! I recently started learning Kotlin and ran into a problem. I have an array of strings, and I need to check if it contains a specific value. In other languages, like Java, I would use a for loop or Arrays.asList().contains(). But in Kotlin, it seems like there should be a more concise way.
Here's an example of my code:
```kotlin
val fruits = arrayOf("apple", "banana", "orange", "grape")
val searchFor = "banana"
```
How can I check if the `fruits` array contains the string `"banana"`? I tried writing `fruits.contains("banana")`, but the compiler gave an error. Then I tried `fruits.indexOf("banana") != -1`, but that didn't work either.
I looked at the documentation, but there are many different functions, and I got confused. Kotlin has the `in` operator, but it only works with collections, not with
No answers yet.
Log in to leave an answer
Blogs
Swift in 2025: Overview of Major New Features and Changes
Generics in JavaScript: Why They Are Needed and How to Use Them for Beginners
Asynchronous Programming in JavaScript: A Complete Guide for Beginners
Rust Ecosystem: Top 10 Libraries for Real Projects