Home > OS >  find string within list of strings
find string within list of strings

Time:10-12

validValueType.ValueTypeGroup

["\"is_enabled\": false", "\"value\":\"OUT\""]
    
failedRecord.record
{"email":"[email protected]","source":"web","value":"OUT","reate_date":"2022-09-29T03:42:09.976-05:00","is_undeliverable":false}
    
        fun publishAlert(failedRecord: Record<String>) {
            if (validValueType.ValueTypeGroup.contains(failedRecord.record)) {
             // do stuff
    
            } else {
                // no match do other stuff
            }
        }

In the list above there are two strings I want to check for when this function receives a record.

The failedRecord.record string does contain what I want "value":"OUT" and it's also within the list above. So why is contains not working here? it keeps bouncing out to the else statement.

CodePudding user response:

You can use any() in the list and pass a predicate:
{x -> searchString.contains(x)}

The searchString.contains() will search x as a substring inside searchString for each x representing the elements in the list

var list = listOf("\"is_enabled\": false", "\"value\":\"OUT\"")
println(list)
println(list::class.qualifiedName)
println() // empty newline
    
var searchString = "{\"email\":\"[email protected]\",\"source\":\"web\",\"value\":\"OUT\",\"create_date\":\"2022-09-29T03:42:09.976-05:00\",\"is_undeliverable\":false}";
println(searchString)
println(searchString::class.qualifiedName)
println() // empty newline
    
println(list.any{x -> searchString.contains(x)});

Output

["is_enabled": false, "value":"OUT"]
java.util.Arrays.ArrayList

{"email":"[email protected]","source":"web","value":"OUT","create_date":"2022-09-29T03:42:09.976-05:00","is_undeliverable":false}
kotlin.String

true
  • Related