Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

String Functions

WCL’s string functions operate on string values and return strings, booleans, lists, or integers depending on the operation. All are pure and side-effect free.

Reference

FunctionSignatureDescription
upperupper(s: string) -> stringConvert to uppercase
lowerlower(s: string) -> stringConvert to lowercase
trimtrim(s: string) -> stringRemove leading and trailing whitespace
trim_prefixtrim_prefix(s: string, prefix: string) -> stringRemove prefix if present
trim_suffixtrim_suffix(s: string, suffix: string) -> stringRemove suffix if present
replacereplace(s: string, from: string, to: string) -> stringReplace all occurrences of from with to
splitsplit(s: string, sep: string) -> listSplit on separator, returning a list of strings
joinjoin(list: list, sep: string) -> stringJoin a list of strings with a separator
starts_withstarts_with(s: string, prefix: string) -> boolTrue if s starts with prefix
ends_withends_with(s: string, suffix: string) -> boolTrue if s ends with suffix
containscontains(s: string, sub: string) -> boolTrue if s contains sub
lengthlength(s: string) -> intNumber of characters (Unicode code points)
substrsubstr(s: string, start: int, end: int) -> stringSubstring from start (inclusive) to end (exclusive)
formatformat(template: string, ...args) -> stringFormat string with {} placeholders
regex_matchregex_match(s: string, pattern: string) -> boolTrue if s matches the regex pattern
regex_captureregex_capture(s: string, pattern: string) -> listList of capture groups from the first match

Examples

upper / lower

let name = "Hello World"
let up = upper(name)     // "HELLO WORLD"
let lo = lower(name)     // "hello world"

trim / trim_prefix / trim_suffix

let padded = "  hello  "
let clean = trim(padded)                         // "hello"
let path = trim_prefix("/api/v1/users", "/api")  // "/v1/users"
let file = trim_suffix("report.csv", ".csv")     // "report"

replace

let msg = replace("foo bar foo", "foo", "baz")  // "baz bar baz"

split / join

let parts = split("a,b,c", ",")      // ["a", "b", "c"]
let rejoined = join(parts, " | ")    // "a | b | c"

starts_with / ends_with / contains

let url = "https://example.com/api"
let secure = starts_with(url, "https")   // true
let is_api = ends_with(url, "/api")      // true
let has_ex = contains(url, "example")   // true

length / substr

let s = "abcdef"
let n = length(s)          // 6
let sub = substr(s, 1, 4)  // "bcd"

format

let msg = format("Hello, {}! You have {} messages.", "Alice", 3)
// "Hello, Alice! You have 3 messages."

Placeholders are filled left to right. Each {} consumes one argument.

regex_match

let valid = regex_match("user@example.com", "^[\\w.]+@[\\w.]+\\.[a-z]{2,}$")
// true

regex_capture

let groups = regex_capture("2024-03-15", "(\\d{4})-(\\d{2})-(\\d{2})")
// ["2024", "03", "15"]

Returns an empty list if there is no match. The list contains only the capture groups, not the full match.

String Interpolation

In addition to these functions, WCL supports ${} interpolation inside string literals and block IDs:

let env = "prod"
let tag = "deploy-${env}"   // "deploy-prod"

Interpolation converts any value to its string representation automatically.