How to check if one value contains another

From MiniScript Wiki
Jump to navigation Jump to search

If you need to check whether a string contains a given substring, or a list or map contains a specified value, then you can use indexOf and compare the result to null.

Example

if "Banana Pudding".indexOf("Ban") != null then
    print "Yep!"
end if

Note that if this is something you need frequently, you might define a "contains" method as follows:

list.contains = function(self, value)
    return self.indexOf(value) != null
end function
string.contains = @list.contains
map.contains = @list.contains

This will work on any string, list, or map, for example:

"Hello world!".contains("lo")  // returns 1