Strings
Zeus provides a first-class string type for working with text. Strings are immutable sequences of UTF-8 encoded bytes.
String Literals
String literals are enclosed in double quotes and create string objects:
let greeting: string = "Hello, Zeus!";let emoji: string = "Welcome 👋";let japanese: string = "こんにちは";String literals fully support UTF-8, including multi-byte characters and emojis.
Escape Sequences
String literals support escape sequences for special characters:
| Escape | Character |
|---|---|
\n | Newline |
\t | Tab |
\r | Carriage return |
\\ | Backslash |
\" | Double quote |
\' | Single quote |
\0 | Null character |
function main(): void { log("Line 1\nLine 2"); // Two lines log("Col1\tCol2\tCol3"); // Tab-separated log("Say \"Hello\""); // Embedded quotes log("Path: C:\\Users"); // Backslash}Output:
Line 1Line 2Col1 Col2 Col3Say "Hello"Path: C:\UsersString vs u8[]
Zeus has two ways to work with text:
| Type | Mutability | Use Case |
|---|---|---|
string | Immutable | Text that shouldn’t change |
u8[] | Mutable | When you need to modify bytes |
Creating Strings
// String literal creates an immutable stringlet message: string = "Hello";Converting to Mutable Bytes
When you assign a string to a u8[], Zeus creates a copy of the underlying bytes. This copy is mutable:
let original: string = "Hello";
// Creates a mutable copy of the byteslet bytes: u8[] = original;
// Modify the copy (original is unchanged)bytes[0] = 'h';
log(original); // "Hello" (unchanged)
// To log bytes, convert back to stringlet modified: string = bytes;log(modified); // "hello"Properties
length
Returns the length of the string in bytes (not characters).
let text: string = "Hello";let len: i32 = text.length; // 5
let emoji: string = "👋";let emojiLen: i32 = emoji.length; // 4 (UTF-8 bytes)Methods
compare
Compares two strings lexicographically (byte-by-byte).
compare(other: string): i8Returns:
-1if this string comes beforeother0if the strings are equal1if this string comes afterother
let a: string = "apple";let b: string = "banana";let c: string = "apple";
let cmp1: i8 = a.compare(b); // -1 (apple < banana)let cmp2: i8 = b.compare(a); // 1 (banana > apple)let cmp3: i8 = a.compare(c); // 0 (apple == apple)equals
Checks if two strings are equal.
equals(other: string): booleanReturns: true if the strings have the same bytes, false otherwise.
let a: string = "hello";let b: string = "hello";let c: string = "world";
if (a.equals(b)) { // This executes - strings are equal}
if (a.equals(c) == false) { // This executes - strings are different}concat
Concatenates two strings and returns a new string.
concat(other: string): stringReturns: A new string containing both strings joined together.
let hello: string = "Hello";let world: string = "World";
let greeting: string = hello.concat(" ").concat(world);log(greeting); // "Hello World"String Operators
Zeus supports operators for string manipulation and comparison:
Concatenation (+)
The + operator concatenates two strings:
let first: string = "Hello";let second: string = "World";let result: string = first + " " + second;log(result); // "Hello World"Equality Operators (== and !=)
Compare strings for equality or inequality:
let a: string = "hello";let b: string = "hello";let c: string = "world";
if (a == b) { log("a equals b"); // This executes}
if (a != c) { log("a not equals c"); // This executes}Comparison Operators (<, >, <=, >=)
Compare strings lexicographically (dictionary order):
let apple: string = "apple";let banana: string = "banana";
if (apple < banana) { log("apple comes before banana"); // This executes}
if (banana > apple) { log("banana comes after apple"); // This executes}
let a: string = "hello";let b: string = "hello";
if (a <= b) { log("a <= b"); // This executes (they're equal)}
if (a >= b) { log("a >= b"); // This executes (they're equal)}Indexing
You can read individual bytes from a string using indexing:
let greeting: string = "Hello";
let h: u8 = greeting[0]; // 72 ('H')let e: u8 = greeting[1]; // 101 ('e')let o: u8 = greeting[4]; // 111 ('o')Since strings are immutable, you cannot assign to a string index:
let text: string = "Hello";text[0] = 'h'; // Error: cannot assign to string index: strings are immutableTo modify characters, convert to u8[] first:
let text: string = "Hello";let bytes: u8[] = text; // Create mutable copybytes[0] = 'h'; // Modify the copylet modified: string = bytes; // "hello"Implicit Conversions
Zeus supports implicit conversion between string and u8[]:
string → u8[]
Creates a mutable copy of the string’s bytes:
let text: string = "Hello";let bytes: u8[] = text; // Mutable copybytes[0] = 'J'; // Modify the copyu8[] → string
Creates a new immutable string from the bytes:
let bytes: u8[] = new u8[];bytes[0] = 'H';bytes[1] = 'i';
let text: string = bytes; // Creates immutable string "Hi"Working with Bytes
Since strings are UTF-8 encoded, you can work with individual bytes through u8[]:
function main(): void { let greeting: string = "Hi!";
// Convert to mutable bytes let bytes: u8[] = greeting;
// Access individual bytes let h: u8 = bytes[0]; // 72 ('H') let i: u8 = bytes[1]; // 105 ('i') let exclaim: u8 = bytes[2]; // 33 ('!')
// Modify bytes bytes[0] = 'h'; // lowercase
// Convert back to string for output let modified: string = bytes; log(modified); // "hi!"}UTF-8 Encoding
Strings use UTF-8 encoding, where characters can be 1-4 bytes:
| Character | Bytes | Example |
|---|---|---|
| ASCII | 1 byte | 'A' = 65 |
| Extended Latin | 2 bytes | 'é' |
| CJK | 3 bytes | '日' |
| Emoji | 4 bytes | '👋' |
let ascii: string = "A";let asciiLen: i32 = ascii.length; // 1
let emoji: string = "👋";let emojiLen: i32 = emoji.length; // 4Future Features
The following features are planned for future releases:
- Substring extraction
- Search and replace
- String interpolation (
${expression}) - Character iteration