Quirrel4.41.0

string.startswith

Defined in module "string"
from "string" import startswith
pure fastcall startswith(str: string, prefix: string): bool

Returns whether str begins with prefix.

Parameters

strstringthe string to test
prefixstringthe prefix to look for

Return value

true when the first prefix.len() characters of str equal prefix, false otherwise.

Notes

An empty prefix always matches: every string starts with the empty string. A prefix longer than str cannot match and gives false without reading past the end of str.

The comparison is a raw byte compare; there is no case-insensitive form.

Example

examples/string/startswith.nut
from "string" import startswith

println("startswith(\"hello world\", \"hello\") =", startswith("hello world", "hello"))
println("startswith(\"hello\", \"hello world\") =", startswith("hello", "hello world")) // prefix longer than str: always false

// the empty prefix always matches
println("startswith(\"anything\", \"\") =", startswith("anything", ""))
Output:
startswith("hello world", "hello") = true
startswith("hello", "hello world") = false
startswith("anything", "") = true

See also

endswithReturns whether str ends with suffix.
stringmodule index