Skip to content
Kavachकवच

Strings

Also known as: string substring concat format trim case uppercase lowercase split join parse search

● Beginner4 minBefore this: VariablesSpec 2.1

A String is text: letters, digits, spaces, any script. Write it in double quotes.

fn main() {
    const name = "Asha"
    print("Hello, {name}!")
    print(name.len())
    print(name.upper())
}

The program ran and printed:

kavach run overview.kav

Hello, Asha!4ASHA
overview.kav: Kavach code, then what running it prints

Braces inside text put a value in: "Hello, {name}!". Methods do the rest: len() counts characters, upper() gives capitals.

Strings never change

name.upper() gives a new string and leaves name as it was. That is why the methods can be chained safely, one after another:

fn main() {
    const s = "  Namaste  "
    print(s.trim())
    print(s.trim().upper())
    print(s.trim().lower())
}

The program ran and printed:

kavach run clean.kav

NamasteNAMASTEnamaste
clean.kav: Kavach code, then what running it prints

Each form below has its own page: making strings, putting values in, joining, length, searching, splitting, and reading numbers.

Limits and sizes

WhatLimit or sizeSource
StringUTF-8 text, as long as memory allows; it can't be changed in place01-built-in-types.md
s.len()counts characters, not bytes: "नमस्ते".len() is 602-built-in-methods.md
Charone Unicode character: code points 0 to 0x10FFFF, surrogates excluded01-built-in-types.md

Every limit in Kavach, on one page: Limits and Sizes.

One page for each form

Try it yourself

Take " kavach ", remove the spaces around it, and print it in capitals.
Show the answer

print(" kavach ".trim().upper()) prints KAVACH.

Go deeper the why, for readers who want it

Because a string never changes, it can be shared instead of copied: handing text to a function or keeping it in a list doesn't copy its characters. Building, splitting and searching large text, against C++:

text — build, split and search large text · WSL Ubuntu 24.04 · i7-13700HX · kavach build (LLVM) · g++ 13 -O2 · Go 1.23 · .NET 8 (C# start-up included) · best of 9

Kavach38 ms · finished 1st
C++ -O271 ms · 1.9× behind
Go109 ms · 2.9× behind
C#257 ms · 6.8× behind
Verify itgit clone https://github.com/Mithlesh04/kavach && cd kavach sh scripts/run-benchmarks.sh "" benchmarks/loops