String Methods Reference

String Methods Reference

Case Conversion

MethodSignatureDescription
.lower() -> stringConvert to lowercase
.upper() -> stringConvert to uppercase
"Hello World" -> .lower              # "hello world"
"Hello World" -> .upper              # "HELLO WORLD"

Prefix and Suffix

MethodSignatureDescription
.starts_with(prefix: string) -> boolTrue if string starts with prefix
.ends_with(suffix: string) -> boolTrue if string ends with suffix
"hello" -> .starts_with("he")        # true
"file.txt" -> .ends_with(".txt")     # true
"Hello" -> .starts_with("hello")     # false (case sensitive)

Search and Position

MethodSignatureDescription
.contains(substr: string) -> boolTrue if string contains substring
.index_of(substr: string) -> numberPosition of first match (-1 if none)
"hello world" -> .contains("world")  # true
"hello world" -> .index_of("o")      # 4
"hello" -> .index_of("x")            # -1

Pattern Matching

MethodSignatureDescription
.match(pattern: string) -> dictFirst regex match info, or [:] if none
.is_match(pattern: string) -> boolTrue if regex matches anywhere

.match Return Value

Returns a dict with three fields:

  • matched: The matched text
  • index: Position of match in string
  • groups: Capture groups as list
"hello123" -> .match("[0-9]+")
# [matched: "123", index: 5, groups: []]

"v1.2.3" -> .match("v(\\d+)\\.(\\d+)\\.(\\d+)")
# [matched: "v1.2.3", index: 0, groups: ["1", "2", "3"]]

"hello" -> .match("[0-9]+")
# [:] (empty dict = no match)

.is_match for Boolean Checks

"hello123" -> .is_match("[0-9]+")    # true
"hello" -> .is_match("[0-9]+")       # false

Pattern Matching in Conditionals

$response -> .is_match("ERROR") ? handle_error()

$response -> .match("code: (\\d+)") => $m
$m -> !.empty ? process($m.groups[0])

Replacement

MethodSignatureDescription
.replace(pattern: string, replacement: string) -> stringReplace first regex match
.replace_all(pattern: string, replacement: string) -> stringReplace all regex matches
"a-b-c" -> .replace("-", "_")        # "a_b-c"
"a-b-c" -> .replace_all("-", "_")    # "a_b_c"

"a1b2c3" -> .replace("[0-9]", "X")       # "aXb2c3"
"a1b2c3" -> .replace_all("[0-9]", "X")   # "aXbXcX"

"hello" -> .replace_all("l", "")     # "heo"

Formatting

MethodSignatureDescription
.trim() -> stringRemove leading/trailing whitespace
.repeat(n: number) -> stringRepeat string n times
.pad_start(length: number, fill: string = " ") -> stringPad start to length
.pad_end(length: number, fill: string = " ") -> stringPad end to length
"  hello  " -> .trim                 # "hello"

"ab" -> .repeat(3)                   # "ababab"
"ab" -> .repeat(0)                   # ""

"42" -> .pad_start(5)                # "   42"
"42" -> .pad_start(5, "0")           # "00042"

"42" -> .pad_end(5)                  # "42   "
"42" -> .pad_end(5, "0")             # "42000"

Splitting and Joining

MethodSignatureDescription
.split(sep: string = "\n") -> listSplit by separator
.join(sep: string = ",") -> stringJoin list with separator
.lines() -> listSplit on newlines (same as .split)
"a,b,c" -> .split(",")               # ["a", "b", "c"]
"a\nb\nc" -> .lines                  # ["a", "b", "c"]

["a", "b", "c"] -> .join("-")        # "a-b-c"
["a", "b", "c"] -> .join("\n")       # "a\nb\nc"

Conversion and Length

MethodSignatureDescription
.str() -> stringConvert any value to string
.num() -> numberParse string to number
.len() -> numberString length
42 -> .str                           # "42"
"42" -> .num                         # 42
"hello" -> .len                      # 5

Element Access

MethodSignatureDescription
.head-> stringFirst character (errors on empty)
.tail-> stringLast character (errors on empty)
.at(index: number) -> stringCharacter at index
"hello" -> .head                     # "h"
"hello" -> .tail                     # "o"
"hello" -> .at(1)                    # "e"

Common Patterns

Normalize and Compare

$input -> .trim -> .lower -> .eq("yes")

Extract and Validate

$email -> .is_match("^[^@]+@[^@]+$") ? process($email) ! { error "Invalid email" }

Format Output

[[name: "Alice", value: 100], [name: "Bob", value: 42]] => $items
$items -> each {
  $.name -> .pad_end(20) => $name
  $.value -> .str -> .pad_start(10) => $val
  "{$name}{$val}"
} -> .join("\n")

Replace Patterns

"hello   world" => $text
$text -> .replace_all("\\s+", " ") -> .trim

Parse Structured Text

"key: value" => $line
$line -> .match("(\\w+):\\s*(.+)") => $m
$m -> !.empty ? [key: $m.groups[0], value: $m.groups[1]]

See Also