String methods

The complete list of methods available on every string value.

Summary

String methods cover case conversion, trimming, searching, splitting, replacing, padding, classification, and parsing. .len() counts Unicode codepoints, not bytes; use .bytes().len() for raw byte length. Indexing with s[i] works in bytes; negative indices count from the end and out-of-bounds returns null. Several methods have aliases: .find() / .index_of(), .ltrim() / .trim_start(), etc.

Canonical

-- case conversion
"hello".upper()                  -- "HELLO"
"HELLO".lower()                  -- "hello"
"hello world".title()            -- "Hello World"
"hello".capitalize()             -- "Hello"

-- trimming
"  hi  ".trim()                  -- "hi"
"  hi".trim_start()              -- "hi"
"hi  ".trim_end()                -- "hi"

-- searching
"hello world".contains("world")  -- true
"hello".starts_with("hel")       -- true
"hello".ends_with("llo")         -- true
"hello".find("ll")               -- 2  (index, or -1)
"hello".rfind("l")               -- 3  (last occurrence)
"hi hi hi".count("hi")           -- 3
"hello".index_of("ll")           -- 2  (alias for find)

-- transformations
"a,b,c".split(",")               -- ["a", "b", "c"]
"hello".replace("l", "r")        -- "herro"
"hello".chars()                  -- ["h", "e", "l", "l", "o"]
"hello".len()                    -- 5
"ha".repeat(3)                   -- "hahaha"
"hello".slice(1, 3)              -- "el"
"hello".reverse()                -- "olleh"
"ab".bytes()                     -- [97, 98]
"one\ntwo".lines()               -- ["one", "two"]

-- padding
"hi".pad_left(5, ".")            -- "...hi"
"hi".pad_right(5, ".")           -- "hi..."
"hi".center(6, ".")              -- "..hi.."

-- prefix/suffix removal
"hello".remove_prefix("hel")     -- "lo"
"hello".remove_suffix("llo")     -- "he"

-- classification
"42".is_digit()                  -- true
"abc".is_alpha()                 -- true
"abc123".is_alnum()              -- true
"ABC".is_upper()                 -- true
"abc".is_lower()                 -- true
"   ".is_space()                 -- true (alias: is_whitespace)
"".is_empty()                    -- true

-- case-flipping
"Hello".swap_case()              -- "hELLO"

-- parsing
"42".parse_int()                 -- 42
"3.14".parse_float()             -- 3.14
"FF".parse_int(16)               -- 255

-- splitting
"hello".split_at(2)              -- ("he", "llo")
"hello".char_at(1)               -- "e"

-- truncation (total length including suffix)
"long text".truncate(7, "...")   -- "long..."
"long text".truncate(4)          -- "l..."

-- joining (called on the separator)
",".join(["a", "b", "c"])        -- "a,b,c"

`.len()` counts codepoints. For raw byte length use .bytes().len(); for ASCII the two are equal.

String indexing (`s[i]`) returns a one-byte string. .bytes() and s[i] work in bytes; .len(), .chars() and .char_at() work in codepoints. Negative indices count from the end. Out-of-bounds returns null.

Method aliases:

  • .find() / .index_of()
  • .trim_start() / .ltrim()
  • .trim_end() / .rtrim()
  • .pad_left() / .lpad() / .pad_start()
  • .pad_right() / .rpad() / .pad_end()
  • .parse_int() / .to_int() / .as_int()