Zyn0.1.0

Strings

A String is a piece of text. Strings are written in double quotes and never change: functions like uppercase() return a new string.

let greeting = "Hello"
println(greeting.length)
println(greeting.uppercase())
println(greeting + ", world")
println(greeting[0])
Output
5
HELLO
Hello, world
H

#String interpolation

An expression inside { } is replaced by its value. There's no $: just braces.

let name = "Ada"
let born = 1815
println("{name} was born in {born}")
println("{name} would be {2026 - born} years old")
println("Her name in capitals: {name.uppercase()}")
Output
Ada was born in 1815
Ada would be 211 years old
Her name in capitals: ADA

Any expression works inside the braces, including calls and other strings:

let scores = ["Ada": 9, "Grace": 10]
println("Best: {scores.filter { _, s -> s == 10 }.keys}")
println("Ada scored {scores["Ada"] ?? 0}")
Output
Best: [Grace]
Ada scored 9

To write a brace itself, put a backslash before it:

println("A set looks like \{1, 2, 3\}")
Output
A set looks like {1, 2, 3}

#Escapes

EscapeMeans
\nNew line
\tTab
\""
\\\
\{ and \}{ and }
println("one\ttwo\nthree \"quoted\" back\\slash")
Output
one	two
three "quoted" back\slash

#Multi-line strings

Three double quotes start a string that can span lines. The indentation the lines share is removed, as are the first and last line breaks:

let name = "Zyn"
let poem = """
    Roses are red,
    {name} is new,
    this string has three lines.
    """
println(poem)
Output
Roses are red,
Zyn is new,
this string has three lines.

#Characters of a string

s[i] is the character at index i (from 0). A for loop goes over the characters:

let word = "héllo"
println(word[1])
for c in word {
    print(c.uppercase())
}
println()
println(word.toList())
Output
é
HÉLLO
[h, é, l, l, o]

#Common functions

let s = "  Zyn is fun  "
println(s.trim())
println(s.contains("fun"))
println(s.trim().startsWith("Zyn"))
println(s.trim().replace("fun", "great"))
println("a,b,c".split(","))
println("ab".repeat(3))
println("Zyn".reversed())
println("hello world".substring(6))
println("hello".indexOf("l"))
Output
Zyn is fun
true
true
Zyn is great
[a, b, c]
ababab
nyZ
world
2

The full list is in the standard library.

#Converting

Anything can become a string with interpolation or toString(). Strings become numbers with toInt() and toFloat(), which return a Result, or with toIntOrNull() and toFloatOrNull(), which return null if the text isn't a number:

println("42".toIntOrNull())
println("4x2".toIntOrNull())
println("2.5".toFloat())
println("abc".toInt())
println(12.toString().length)
Output
42
null
Ok(2.5)
Err('abc' is not a whole number)
2

#Comparing

== compares the text; < and > compare alphabetically (by Unicode value).

println("zyn" == "zy" + "n")
println("apple" < "banana")
println("Zebra" < "apple")
Output
true
true
true