Compare strings with contains

Hello friends!

I would like to compare my news strings that contains a keyword from Item VoiceSearch. If some any of my News item strings contains the VoiceSearch result then say the string result.

String VoiceSearch

Strings from RSS-feeds
String Apple1 “[%s]” (NewsSense)
String Apple2 “[%s]” (NewsSense)
String Apple3 “[%s]” (NewsSense)

How should I do this?

In a rule:

    val List<String> matches = newArrayList // you will need to import java.util.List

    NewsSense.members.forEach[ news |
        if(news.state.toString.contains(VoiceSearch.state.toString)) {

    // matches now contains all the text from the NewsSense Items that contain the String in VoiceSearch

Thank you very, very much! Now I have work to do. :slight_smile:

One more stupid question. How do I call/get the [quote=“rlkoshak, post:2, topic:15760”]
[/quote] results and [quote=“rlkoshak, post:2, topic:15760”]
[/quote] - is that a Item?

matches is a list. You can get the results one by one using matches.get(0) through matches.get(matches.size-1) or iterate through them again using matches.forEach[ match | do something].

“news” is just a variable name so I can reference the current thing from the list. In another language or pseudocode the above might looks like:

for each StringItem news in NewSense.members do:
    if(news.state.toString.contains ...

The body of the for loop will execute on each and every thing in members. “news” is just a way to refer to the current thing we are iterating over. You could call it anything you want.

This is the code. What is missing to make the say-command work?

rule "Voice control Search Keyword"
when Item VoiceSearch changed
say("Okej, I will search for " + VoiceSearch.state,"Klara"){
val List<String> matches = newArrayList
NewsSense.members.forEach[ news |
    if(news.state.toString.contains(VoiceSearch.state.toString)) {
    // matches now contains all the text from the NewsSense Items that contain the String in VoiceSearch
say("I found  " + Info.state, "Klara")}

I can’t help with that. I’ve never use the say Action.

The say-action is no problem. I can’t get the matches to Info.state. I get the error

The name '<XFeatureCallImplCustom>.put(<XMemberFeatureCallImplCustom>)' cannot be resolved to an item or type.

My bad. The method is add, not put.

1 Like

Strange. The same error occurred with


Can it be


that´s the culprit? I want to put the


in the


for the say-command

say("I found  " + Info.state, "Klara")}


I recommend spending some time reading the OH wiki and OH docs because that statement makes absolutely no sense.

Let me break it down:

val lastString = matches.get(matches.size-1)
lastString.put(Info.state) // lastString is a String, it has no put method. Info.state is not a String so even if there was a put method this wouldn't work, furthermore, this would add Info.state to lastString, not set Info.state to lastString

The correct way to do this is:

val lastMatch = matches.last
say("I found " + lastMatch,  " Klara")
1 Like

Thanks! I will try this. I am pretty new to .get .put and newArrayList.

Yes! It works! Thank you so much @rlkoshak!