Skip to content

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.

String vs u8[]

Zeus has two ways to work with text:

TypeMutabilityUse Case
stringImmutableText that shouldn’t change
u8[]MutableWhen you need to modify bytes

Creating Strings

// String literal creates an immutable string
let 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 bytes
let bytes: u8[] = original;
// Modify the copy (original is unchanged)
bytes[0] = 'h';
log(original); // "Hello" (unchanged)
// To log bytes, convert back to string
let 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): i8

Returns:

  • -1 if this string comes before other
  • 0 if the strings are equal
  • 1 if this string comes after other
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): boolean

Returns: 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
}

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 copy
bytes[0] = 'J'; // Modify the copy

u8[] → 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:

CharacterBytesExample
ASCII1 byte'A' = 65
Extended Latin2 bytes'é'
CJK3 bytes'日'
Emoji4 bytes'👋'
let ascii: string = "A";
let asciiLen: i32 = ascii.length; // 1
let emoji: string = "👋";
let emojiLen: i32 = emoji.length; // 4

Future Features

The following features are planned for future releases:

  • String concatenation
  • Substring extraction
  • Search and replace
  • String interpolation (${expression})
  • Character iteration

Next

Arrays →