The string library provides functions for transforming, searching, and formatting strings.
include "string";All functions treat strings as sequences of bytes. They do not perform Unicode-aware operations.
Converts every character in s to uppercase.
string.upper("hello"); // "HELLO"Converts every character in s to lowercase.
string.lower("HELLO"); // "hello"Removes leading and trailing whitespace from s.
string.trim(" hello "); // "hello"Removes leading whitespace only.
Removes trailing whitespace only.
Returns true if s contains sub.
string.contains("hello world", "world"); // trueReturns true if s begins with prefix.
string.starts_with("hello", "he"); // trueReturns true if s ends with suffix.
string.ends_with("hello", "lo"); // trueReturns the zero-based index of the first occurrence of sub in s.
string.find("hello", "ll"); // 2
string.find("hello", "z"); // -1Returns: The index as a num, or -1 if not found.
Extracts a substring starting at index start. If len is provided, at most len characters are returned.
string.substr("hello world", 6); // "world"
string.substr("hello world", 6, 3); // "wor"Returns: The extracted substring, or an empty string if start is out of range.
Replaces every occurrence of from in s with to.
string.replace("aabbcc", "b", "x"); // "aaxxcc"Returns: The resulting string.
Splits s into an array of substrings, cutting at each occurrence of delimiter.
string.split("a,b,c", ","); // ["a", "b", "c"]Returns: An array of strings. Returns the original string unchanged if delimiter is empty.
Joins an array of strings into a single string, inserting delimiter between each element.
string.join(["a", "b", "c"], "-"); // "a-b-c"Returns: The concatenated string.
Replaces each {} placeholder in template with the next argument, in order.
string.format("Hello, {}! You are {} years old.", "Alice", 30);
// "Hello, Alice! You are 30 years old."Use {{ and }} to include a literal brace in the output.
string.format("{{{}}}", "x"); // "{x}"Throws: If the number of {} placeholders does not match the number of arguments.
include "string";
pin csv = "alice,30,engineer";
pin parts = string.split(csv, ",");
print(string.upper(parts[0])); // "ALICE"
print(string.format("Name: {}, Age: {}, Role: {}", parts[0], parts[1], parts[2]));