Strings and Runes

9.1 String Basics

Strings are immutable, UTF-8 encoded byte arrays. s.len is the byte count.

s := 'Hello, V!'
println(s.len)          // 9
println(s[0])           // 72 (a u8 byte)
println(s[0].ascii_str())  // 'H'
println(s[0..5])        // 'Hello' (slice)
println(s[7..])         // 'V!'
// s[0] = `h`           // error: strings are immutable

9.2 Interpolation and Concatenation

name := 'Ana'
age := 30
println('${name} is ${age} years old')
println('Next year: ${age + 1}')
println('Pi: ${math.pi:.2f}')          // formatting
println('[${name:10}]')                // width (right-aligned)
println('[${name:-10}]')               // left-aligned
println('${255:x} ${255:X} ${10:08b} ${42:05}')   // hex, binary, zero-padded
full := 'Hello, ' + name               // concatenation
mut sb := strings.new_builder(100)     // efficient building
sb.write_string('a')
sb.write_string('b')
println(sb.str())

9.3 Common String Methods

s := '  Hello World  '
println(s.trim_space())
println(s.to_upper())
println(s.to_lower())
println('a,b,c'.split(','))
println(['a', 'b'].join('-'))
println('hello'.contains('ell'))
println('hello'.starts_with('he'))
println('hello'.ends_with('lo'))
println('hello'.index('l') or { -1 })
println('hello'.replace('l', 'L'))
println('abc'.repeat(3))
println('hello'.reverse())
println('hello world'.title())
println('42'.int() + 1)
println('3.5'.f64())
println('hello'.count('l'))
println('a b  c'.fields())
println('Hello'.bytes())
println('héllo'.runes().len)            // code points
println('x'.is_upper())
println('x=1'.split_once('=') or { ('', '') })
println('hello'.substr(1, 3))           // 'el'
println('hello'.all_after('he'))
println('path/to/file.txt'.all_after_last('/'))

9.4 Runes and Unicode

r := `🌍`
println(r.str())
s := 'héllo 🌍'
for c in s.runes() { print('${c} ') }
for i, b in s { println('${i}: ${b}') }   // iterates bytes
println(s.runes()[1].str())               // 'é'
import encoding.utf8
println(utf8.len(s))

9.5 Regular Expressions

Alternatively, pcre bindings via VPM.

import regex
mut re := regex.regex_opt(r'(\d{4})-(\d{2})-(\d{2})') or { panic(err) }
start, end := re.match_string('2025-10-24')
if start >= 0 {
	println(re.get_group_list())
}