Quirrel4.41.0

types.String.contains

Method on every string
pure string.contains(substr: string, [start: number], ...): any

Returns whether str contains substr.

The binding carries no declaration string, so the VM cannot report parameter names. The names above are from this page; the types and attributes still come from the VM. The VM also cannot tell an optional parameter from a variadic tail here, so read the brackets and any trailing ... from the prose below, not from the signature.

Parameters

substrstringthe substring to look for; must not be empty
startnumberbyte index to start searching from; defaults to 0 optional
...anynot a real parameter; see Notes repeats

Return value

true if substr occurs at or after start, false otherwise - always a bool, never null. This is the difference from types.String.indexof: the same scan, but contains never reports "not found" as anything other than false. A negative or out-of-range start gives false, not a wraparound search.

Errors

Throws empty substring when substr is "".

Notes

Takes 1 or 2 arguments: start is optional. A third argument is silently ignored.

Example

examples/types/string/contains.nut
println("\"hello\".contains(\"ell\") =", "hello".contains("ell"))
println("\"hello\".contains(\"xyz\") =", "hello".contains("xyz"))

// unlike indexof, a negative start does not wrap: it just cannot match.
println("\"hello\".contains(\"l\", -2) =", "hello".contains("l", -2))
Output:
"hello".contains("ell") = true
"hello".contains("xyz") = false
"hello".contains("l", -2) = false

See also

indexofReturns the byte index of the first occurrence of substr in str.
startswithReturns whether str begins with prefix.
stringclass index