starts_with
starts_with checks whether a string begins with a given prefix.
Overview
:starts_with returns true if the string begins with the given prefix, false otherwise.
:starts_with("hello world", "hello") /// true :starts_with("hello world", "world") /// false
:starts_with("hello world", "hello") /// true :starts_with("hello world", "world") /// false
Basic Usage
url | "https://sheriffcloud.com" if :starts_with(url, "https://") :say("secure") xx
url | "https://sheriffcloud.com" if :starts_with(url, "https://") :say("secure") xx
Multiple Prefixes
Pass an array to check against several prefixes at once. Returns true if the string starts with any of them.
url | "https://sheriffcloud.com" if :starts_with(url, ["http://", "https://"]) :say("is a web link") xx
url | "https://sheriffcloud.com" if :starts_with(url, ["http://", "https://"]) :say("is a web link") xx
This replaces chained or conditions:
/// before if :starts_with(url, "http://") or :starts_with(url, "https://") /// after if :starts_with(url, ["http://", "https://"])
/// before if :starts_with(url, "http://") or :starts_with(url, "https://") /// after if :starts_with(url, ["http://", "https://"])
In a Judge Dispatcher
:starts_with is especially useful as a judge condition when dispatching by prefix:
judge :starts_with(inner, "image:"): resolve_image_link(inner, portal) :starts_with(inner, ["http://", "https://"]): resolve_external_link(inner) else: resolve_page_link(inner, ctx, portal) xx
judge :starts_with(inner, "image:"): resolve_image_link(inner, portal) :starts_with(inner, ["http://", "https://"]): resolve_external_link(inner) else: resolve_page_link(inner, ctx, portal) xx
Signature
:starts_with(text, prefix) :starts_with(text, [prefix1, prefix2, ...])
:starts_with(text, prefix) :starts_with(text, [prefix1, prefix2, ...])
| Argument | Type | Description |
|---|---|---|
text |
string | The string to check. |
prefix |
string or array | Prefix or list of prefixes to match. |
Returns bool.