Skip to content

Strings

String is Zap’s owned text type. String literals use double quotes:

var language: String = "Zap";
var message = "Hello";

The automatic prelude provides the common string operations.

The + operator creates a string from strings and characters:

var name = "Ada";
var greeting = "Hello, " + name + '!';
println(greeting);

Convert other values explicitly:

var count = 3;
println("Count: " + toString(count));
var word = "Zap";
var size = len(word);
var first = at(word, 0);
var alsoFirst = word[0];

Indices start at zero. An out-of-range string read returns '\0'.

if eq("zap", "zap") {
println("equal");
}
if startsWith("zaplang", "zap") {
println(toString(indexOf("zaplang", "lang")));
}

The normal comparison operators also work for strings:

var same = "left" == "left";
var ordered = "alpha" < "beta";

StringView can inspect a substring without owning another allocation:

var source = "event:deploy";
var value: StringView = slice(source, 6, len(source) - 6);
println(value);

A view borrows its storage. Its lifetime and function contracts are covered in Borrowed string views.

More helpers and TextBuf are documented in std/string.