Repository navigation
Strings and Strbuf
Frank edited this page Jan 26, 2026
·
6 revisions
"hello"
"line1\nline2"Raw (backtick) strings:
`C:\\temp\\file`
`line1
line2`Notes:
- Backtick strings do not process escapes (backslashes are literal).
- They can span multiple lines.
- String interpolation is not supported inside backtick strings.
- A backtick cannot appear inside a backtick string.
Escapes supported:
\n \t \r \" \\-
\efor ASCII ESC (27) -
\xHHfor a single byte using two hex digits - Unknown escapes become literal character (e.g.
\q→q)
-
int + int→ arithmetic -
If either side is a string → string concatenation
- non-string side is rendered as an integer string in the current implementation (best behavior guaranteed for ints; other objects use a generic representation in some contexts)
Example:
print("x=" + 10);CupidScript supports ${...} expressions inside double-quoted strings.
let name = "Ada";
let count = 3;
print("Hello ${name}, you have ${count} messages.");Notes:
- Any expression is allowed inside
${...}. - Values are converted to strings using the same representation as
to_str(). - Escapes still work inside the literal parts of the string.
Examples:
print("x=${1 + 2}");
print("bool=${true}");
print("nil=${nil}");See Standard-Library.md for:
str_findstr_replacestr_splitstr_containsstr_countstr_pad_startstr_pad_endstr_reversesubstrjointo_strto_int
New string manipulation functions for common operations:
-
str_trim(s)/trim(s)- Remove whitespace from both ends -
str_ltrim(s)/ltrim(s)- Remove leading whitespace -
str_rtrim(s)/rtrim(s)- Remove trailing whitespace
let s = " hello world ";
print(str_trim(s)); // "hello world"
print(str_ltrim(s)); // "hello world "
print(str_rtrim(s)); // " hello world"-
str_lower(s)/lower(s)- Convert to lowercase -
str_upper(s)/upper(s)- Convert to uppercase
print(str_lower("Hello World")); // "hello world"
print(str_upper("Hello World")); // "HELLO WORLD"-
str_startswith(s, prefix)/starts_with(s, prefix)- Check if string starts with prefix -
str_endswith(s, suffix)/ends_with(s, suffix)- Check if string ends with suffix
print(str_startswith("hello.txt", "hello")); // true
print(str_endswith("hello.txt", ".txt")); // true
print(str_startswith("hello.txt", "world")); // false-
str_repeat(s, count)- Repeat string N times -
split_lines(s)- Split into lines on\nor\r\n -
str_contains(s, sub)- Substring check -
str_count(s, sub)- Count occurrences -
str_pad_start(s, width, pad)- Left pad -
str_pad_end(s, width, pad)- Right pad -
str_reverse(s)- Reverse bytes
print(str_repeat("abc", 3)); // "abcabcabc"
print(str_repeat("x", 5)); // "xxxxx"
print(split_lines("a\n b\r\n")); // ["a", " b", ""]
print(str_contains("hello", "ell")); // true
print(str_count("banana", "na")); // 2
print(str_pad_start("7", 4, "0")); // "0007"
print(str_pad_end("7", 4, "0")); // "7000"
print(str_reverse("Cupid")); // "dipuC" (byte-wise)Create:
let b = strbuf();Methods are invoked via "field call" syntax:
b.append("hi");
b.append(123);
b.append(true);
b.append(nil);
print(b.str());
print(b.len());
b.clear();Appends:
- string contents
- integer as decimal
- bool as
true/false - nil as
nil
Anything else may fail (runtime error).
Returns current contents as an immutable string.
Resets buffer to empty.
Returns current length as int.