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
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
Each form below has its own page: making strings, putting values in, joining, length, searching, splitting, and reading numbers.
Limits and sizes
| What | Limit or size | Source |
|---|---|---|
String | UTF-8 text, as long as memory allows; it can't be changed in place | 01-built-in-types.md |
s.len() | counts characters, not bytes: "नमस्ते".len() is 6 | 02-built-in-methods.md |
Char | one Unicode character: code points 0 to 0x10FFFF, surrogates excluded | 01-built-in-types.md |
Every limit in Kavach, on one page: Limits and Sizes.
One page for each form
Try it yourself
" 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
git clone https://github.com/Mithlesh04/kavach && cd kavach
sh scripts/run-benchmarks.sh "" benchmarks/loops