The String library provides robust string manipulation utilities.
String literals are enclosed in double quotes "...". You can use the backslash \ to escape characters:
\"- Double quote\\- Backslash\n- New line\t- Tab\r- Carriage return
Example: print("Hello \"World\""); // Hello "World"
Returns the number of characters in a string.
- Example:
String.length("hello"); // 5
Returns the character at the specified index.
- Example:
String.charAt("abc", 1); // "b"
- Example:
String.toLowerCase("HELLO"); // "hello"
- Example:
String.toUpperCase("hello"); // "HELLO"
Removes leading and trailing whitespace.
- Example:
String.trim(" foo "); // "foo"
Replaces occurrences of a substring.
- Example:
String.replace("apple", "p", "b"); // "abble"
Splits a string into an array.
- Example:
String.split("a,b,c", ","); // ["a", "b", "c"]
- Example:
String.substr("hello", 1, 3); // "ell"
Tests if a string matches a regex pattern. Regex literals use the r"..." prefix and support escaping the double quote \" without interfering with regex backslashes.
- Example:
String.test("abc", r"^[a-z]+$"); // true - Example:
String.test("quote\"", r"\"$"); // true
- Example:
String.reverse("abc"); // "cba"