Reference · Strings
contains
contains(str | list | map | null, any) -> boolWhat it does
Whether a value holds another: in a string, the second argument as text; in a list, an
item equal to it (as == says); in a map, a key with that name. null holds nothing,
so a missing list or text gives false rather than an error.
Examples
print(contains("hello", "ell"), contains("hello", "L"))print(contains([1, 2, 3], 2), contains(["a", "b"], "c"))print(contains({ a: 1 }, "a"), contains({ a: 1 }, 1))true falsetrue falsetrue falseEdge cases
- In a string the search is case-sensitive and the needle is written as text first:
contains("a1b", 1)istrue. Every string contains"". - In a list the match is equality:
2.0equals2, but"2"does not; a list or a map inside is found when it is equal, andnullis found when it is there. - In a map only keys count, as text:
contains({ "1": 1 }, 1)istrue, and a key whose value isnullis still a key. nullas the first argument isfalse.
print(contains("a1b", 1), contains("hello", ""), contains("héllo", "é"))print(contains([1, 2, 3], 2.0), contains([1, 2, 3], "2"), contains([[1], [2]], [1]), contains([null], null), contains([], 1))print(contains({ "1": 1 }, 1), contains({ a: null }, "a"), contains({}, "a"))print(contains(null, "a"))true true truetrue false true true falsetrue true falsefalseErrors
A first argument that is not a string, list, map or null is a runtime error, not an
error value: a number is not searched. Convert it with str first.
print(contains(42, 4))line 1: `contains` expects a str, list or map, got intChaining
In a pipeline the container comes first and the needle is the argument:
print("a b" | contains(" "), ["a", "b"] | contains("a"))print(["ann@x.io", "bob"] | filter((s) => s | contains("@")))print(env("PATH") | contains("/"))true true["ann@x.io"]trueSee also
index_of, starts_with, ends_with, has, any, re.test
Thread 0 posts
On contains(): questions, tips, corrections, and code that uses it.
No posts yet. The first one opens the thread.
Edit this page on GitHub · every example's output was made by running it (tools/refcheck.mjs) · questions on Discord or r/quillo