Quirrel4.41.0

types.String.startswith

Method on every string
string.startswith(prefix: string): any

Returns whether str begins with prefix.

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

prefixstringthe prefix to look for

Return value

true when the first prefix.len() bytes of str equal prefix, false otherwise. An empty prefix always matches; a prefix longer than str cannot match.

Notes

Takes exactly one argument. Same implementation as string.startswith, reached with str and prefix swapped between an explicit first argument and the method receiver; see the Notes on types.String.strip for how the two forms differ only in their reported attributes, not their behavior.

The comparison is a raw byte compare, so it works the same on a multi-byte prefix as on any other bytes; there is no case-insensitive form.

Example

examples/types/string/startswith.nut
println("\"hello world\".startswith(\"hello\") =", "hello world".startswith("hello"))
println("\"hello world\".startswith(\"world\") =", "hello world".startswith("world"))

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

See also

endswithReturns whether str ends with suffix.
startswithReturns whether str begins with prefix.
stringclass index