Reference · Strings

contains

signature
contains(str | list | map | null, any) -> bool

What 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))
output
true falsetrue falsetrue false

Edge cases

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"))
output
true true truetrue false true true falsetrue true falsefalse

Errors

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.

this one fails
print(contains(42, 4))
output
line 1: `contains` expects a str, list or map, got int

Chaining

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("/"))
output
true true["ann@x.io"]true

See also

index_of, starts_with, ends_with, has, any, re.test

Thread
7Useful? Like it, or add to its thread below.

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