Split and Replace
.split(sep) divides the string by a separator and returns a string array.
The separator can be more than one character long. A duplicated delimiter
produces an empty string between the two — the array length reflects this.
.replace(old, new) substitutes all occurrences of old with new and
returns a new string; the original remains untouched. Passing "" as new is
the idiomatic way to remove characters.
Splitting by a simple and a multi-character separator; replacing and removing substrings.
07-split-and-replace.zolo
// Feature: Splitting and replacing
// Syntax: `s.split(sep)` -> [str], `s.replace(old, new)` -> str
// When to use: simple CSV parsing, sanitization, transformations.
use std::csv
use std::log
// -- split: divides the string by a separator -------------------
let csv = "alice,bob,carol"
let names = csv.split(",")
print(names.len()) // 3
print(names[0]) // alice
print(names[1]) // bob
print(names[2]) // carol
// Iterate parts.
for n in csv.split(",") {
print("- {n}")
}
// expected:
// - alice
// - bob
// - carol
// Separator longer than 1 character.
let log = "INFO::ready::server::up"
let pieces = log.split("::")
print(pieces.len()) // 4
print(pieces[0]) // INFO
// Empty string between delimiters becomes an empty piece.
let chunk = "a,,c"
print(chunk.split(",").len()) // 3
// -- replace: substitutes all occurrences ----------------------
let s = "hello world"
print(s.replace("o", "0")) // hell0 w0rld
print(s.replace("world", "Zolo")) // hello Zolo
// Original is unchanged.
print(s) // hello world
// Chain: normalize spacing.
let raw = "a b c"
let clean = raw.replace(" ", " ")
print(clean)
// expected: a b c
// Replace a character with nothing — equivalent to removing.
print("a-b-c".replace("-", ""))
// expected: abc
Challenge
Given the string "2026-05-30", use .split("-") to extract the year, month,
and day into separate variables and print "Day: 30, Month: 05, Year: 2026".