Strings
This content is for v0.7. Switch to the latest version for up-to-date documentation.
String literals
Section titled “String literals”Strings are delimited by double quotes.
let greeting = "Hello, World!";let empty = "";Escape sequences
Section titled “Escape sequences”Use a backslash to insert special characters.
| Sequence | Character |
|---|---|
\" | Double quote |
\\ | Backslash |
\n | Newline |
\r | Carriage return |
\t | Tab |
// Quoted stringslet msg = "She said \"hello\"";
// Backslash in pathslet path = "C:\\Users\\music\\samples";
// Multiline outputPRINT "line one\nline two\nline three";
// Tab-separated columnsPRINT "Track\tNote\tVelocity";PRINT "1\tC4\t100";Concatenation
Section titled “Concatenation”Use + to join strings.
"RESONON" + " " + "Language"; // "RESONON Language"
// Building strings in loopslet result = "";for i in range(5) { result += "* ";}PRINT result; // "* * * * * "Building note names from parts:
let roots = #["C", "D", "E", "F", "G", "A", "B"];let octave = "4";for root in roots { PRINT root + octave; // "C4", "D4", ...}Format strings
Section titled “Format strings”Prefix a string with f to interpolate expressions inside {...}.
let name = "RESONON";let version = 1;PRINT f"Welcome to {name} v{version}!";
// Any expression works inside bracesPRINT f"2 + 2 = {2 + 2}";
// Method callslet lang = "resonon";PRINT f"uppercase: {lang.uppercase()}";
// Literal braces with {{ and }}PRINT f"Literal braces: {{like this}}";Format strings are useful for building readable output:
fn describe(item, count) { f"{count}x {item}"}PRINT describe("kick", 4); // "4x kick"PRINT describe("snare", 2); // "2x snare"Expressions in format strings
Section titled “Expressions in format strings”Any valid expression works inside {...}, including ternary-style if/else, array access, and method chains.
let vel = 100;PRINT f"Dynamics: {if vel > 80 { "loud" } else { "soft" }}";
let notes = #["C4", "E4", "G4"];PRINT f"Root: {notes[0]}";
let raw = " hello ";PRINT f"Trimmed: {raw.trim().uppercase()}";Strings inside expressions
Section titled “Strings inside expressions”String literals work inside f-string expression blocks.
let items = #["kick", "snare", "hat"];PRINT f"Count: {items.iter().count()}";PRINT f"Joined: {items[0] + " & " + items[1]}";String immutability
Section titled “String immutability”Strings are immutable. Every method returns a new string — the original is never modified.
let original = "Hello";let upper = original.uppercase();
PRINT original; // "Hello" — unchangedPRINT upper; // "HELLO"Methods
Section titled “Methods”Methods are called with dot syntax and always return a new value.
| Method | Description | Example |
|---|---|---|
.uppercase() | All characters to upper case | "hello".uppercase() → "HELLO" |
.lowercase() | All characters to lower case | "HELLO".lowercase() → "hello" |
.trim() | Remove leading/trailing whitespace | " hi ".trim() → "hi" |
.contains(sub) | Check if substring exists | "hello".contains("ell") → true |
.starts_with(prefix) | Check prefix | "hello".starts_with("he") → true |
.ends_with(suffix) | Check suffix | "hello".ends_with("lo") → true |
.replace(old, new) | Replace all occurrences | "aabb".replace("a", "x") → "xxbb" |
.substring(start, end) | Extract a sub-string | "hello".substring(1, 4) → "ell" |
.split(sep) | Split into an array | "a,b,c".split(",") → ["a","b","c"] |
.length() | Number of characters | "hello".length() → 5 |
.iter() | Character iterator | "abc".iter().collect() → ["a","b","c"] |
replace
Section titled “replace”Replaces all occurrences of a substring.
let pattern = "x-x-x-x-";pattern.replace("x", "kick"); // "kick-kick-kick-kick-"
// Remove characters by replacing with empty string"C#4".replace("#", ""); // "C4"Splits a string into an array at each occurrence of the separator.
let csv = "kick,snare,hat,tom";let drums = csv.split(",");PRINT drums; // ["kick", "snare", "hat", "tom"]PRINT drums[0]; // "kick"
// Split on whitespace"C4 E4 G4".split(" "); // ["C4", "E4", "G4"]substring
Section titled “substring”Extracts characters from start (inclusive) to end (exclusive). Indices are clamped to the string length.
"hello".substring(0, 1); // "h""hello".substring(1, 4); // "ell"
// Out-of-range end is clamped"hello".substring(3, 100); // "lo"Removes leading and trailing whitespace (spaces, tabs, newlines).
" spaced out ".trim(); // "spaced out""\t\ttabbed\n".trim(); // "tabbed"contains, starts_with, ends_with
Section titled “contains, starts_with, ends_with”Test whether a string includes, begins with, or ends with a given substring. All three return true or false.
let port = "IAC Driver Bus 1";port.contains("Driver"); // trueport.starts_with("IAC"); // trueport.ends_with("Bus 2"); // false
// Filter notes by accidentallet notes = #["C4", "C#4", "D4", "Eb4"];for n in notes { if n.contains("#") || n.contains("b") { PRINT f"{n} is an accidental"; }}Method chaining
Section titled “Method chaining”Because every method returns a new string, calls can be chained:
let messy = " HELLO world ";messy.trim().lowercase(); // "hello world"Case-insensitive search
Section titled “Case-insensitive search”let portName = "IAC Driver Bus 1";let search = "iac driver";if portName.lowercase().contains(search.lowercase()) { PRINT "Match found!";}Iteration
Section titled “Iteration”Strings are not directly iterable. Call .iter() to get a character iterator for use with for-in.
for ch in "RESONON".iter() { PRINT ch;}// R// E// S// O// N// O// NCollect characters into an array:
let chars = "hello".iter().collect();PRINT chars; // ["h", "e", "l", "l", "o"]Iterators
Section titled “Iterators”.iter() yields single-character strings. The returned iterator supports the same lazy and eager methods as array iterators.
// Filter vowelslet vowels = "resonon".iter() .filter(fn(ch) { return ch == "e" || ch == "o"; }) .collect();PRINT vowels; // ["e", "o", "o"]
// Enumerate characterslet pairs = "abc".iter().enumerate().collect();// [[0, "a"], [1, "b"], [2, "c"]]See Arrays — Iterators for the full iterator method reference.