Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test if string contains anything from an array of strings (kotlin)

I'm new to Kotlin (I have a Java background) and I can't seem to figure out how to check whether a string contains a match from a list of keywords.

What I want to do is check if a string contains a match from an array of keywords (case-insensitive please). If so, print out the keyword(s) that was matched and the string that contained the keyword. (I will be looping over a bunch of strings in a file).

Here's an MVE for starters:

val keywords = arrayOf("foo", "bar", "spam")

fun search(content: String) {
    var match = <return an array of the keywords that content contained>
    if(match.size > 0) {
          println("Found match(es): " + match + "\n" + content)
    }
}   

fun main(args: Array<String>) {
    var str = "I found food in the barn"
    search(str) //should print out that foo and bar were a match
}

As a start (this ignores the 'match' variable and getting-a-list-of-keywords-matched), I tried using the following if statement according with what I found at this question,

if(Arrays.stream(keywords).parallel().anyMatch(content::contains))

but it put a squiggly line under "content" and gave me this error

None of the following functions can be called with the arguments supplied: public operator fun CharSequence.contains(char: Char, ignoreCase: Boolean = ...): Boolean defined in kotlin.text public operator fun CharSequence.contains(other: CharSequence, ignoreCase: Boolean = ...): Boolean defined in kotlin.text @InlineOnly public inline operator fun CharSequence.contains(regex: Regex): Boolean defined in kotlin.text

like image 705
takanuva15 Avatar asked Jun 10 '18 23:06

takanuva15


People also ask

How do you check if a string contains a string from an array?

includes() You can use the includes() method in JavaScript to check if an item exists in an array. You can also use it to check if a substring exists within a string. It returns true if the item is found in the array/string and false if the item doesn't exist.

How do you check if a string exists in an array Kotlin?

To check if an Array contains a specified element in Kotlin language, use Array. contains() method. Call contains() method on this array, and pass the specific search element as argument. contains() returns True if the calling array contains the specified element, or False otherwise.


1 Answers

You can use the filter function to leave only those keywords contained in content:

val match = keywords.filter { it in content }

Here match is a List<String>. If you want to get an array in the result, you can add .toTypedArray() call.

in operator in the expression it in content is the same as content.contains(it).

If you want to have case insensitive match, you need to specify ignoreCase parameter when calling contains:

val match = keywords.filter { content.contains(it, ignoreCase = true) }
like image 104
Ilya Avatar answered Oct 26 '22 21:10

Ilya