String utilities.
use "str"str.upper("hello") # HELLO
str.lower("HELLO") # helloRemoves leading and trailing whitespace.
str.trim(" hello ") # helloReturns true if the string contains the substring.
str.contains("hello world", "world") # truestr.starts_with("hello", "he") # true
str.ends_with("hello", "lo") # trueReplaces all occurrences of a substring.
str.replace("hello", "l", "r") # herroSplits a string by a separator. Returns an array.
parts = str.split("a,b,c", ",")
puts parts # ["a", "b", "c"]Returns the index of the first occurrence of a substring, or -1 if not found.
str.index("hello", "ll") # 2
str.index("hello", "xyz") # -1Joins an array into a string with the given separator.
parts = ["a=1", "b=2", "c=3"]
str.join(parts, "&") # "a=1&b=2&c=3"
str.join([1, 2, 3], "-") # "1-2-3"Returns the number of Unicode characters (runes) in a string. Unlike len, which returns byte count, rune_count counts visible characters.
str.rune_count("hello") # 5
str.rune_count("café") # 4 (len returns 5)
str.rune_count("日本語") # 3Counts non-overlapping occurrences of a substring.
str.count("hello world", "l") # 3
str.count("aaa", "aa") # 1Repeats a string n times.
str.repeat("ab", 3) # "ababab"
str.repeat("x", 0) # ""Reverses a string by Unicode characters.
str.reverse("hello") # "olleh"
str.reverse("café") # "éfac"Splits a string into an array of individual characters.
str.chars("hi") # ["h", "i"]
str.chars("café") # ["c", "a", "f", "é"]Splits a string by whitespace into an array of words.
str.fields(" a b c ") # ["a", "b", "c"]Removes a prefix or suffix from a string if present.
str.trim_prefix("hello", "he") # "llo"
str.trim_suffix("hello", "lo") # "hel"Left-pads a string to a given width. Optional third argument is the pad character (default: space).
str.pad_left("hi", 5) # " hi"
str.pad_left("hi", 5, "0") # "000hi"Right-pads a string to a given width. Optional third argument is the pad character (default: space).
str.pad_right("hi", 5) # "hi "
str.pad_right("hi", 5, ".") # "hi..."Centers a string within a given width. Optional third argument is the pad character (default: space).
str.center("hi", 6) # " hi "
str.center("hi", 6, "-") # "--hi--"Splits a string into an array of lines.
lines = str.each_line("a\nb\nc") # ["a", "b", "c"]Returns the index of the last occurrence of a substring, or -1 if not found.
str.last_index("hello", "l") # 3
str.last_index("hello", "xyz") # -1Extracts a substring by rune start and end indices. Supports negative indices (counted from end).
str.slice("hello", 1, 3) # "el"
str.slice("hello", -3, -1) # "ll"
str.slice("café", 3, 4) # "é"Returns true if the string is empty.
str.empty("") # true
str.empty("x") # false