Strings: Searching
● Beginner3 min
contains, starts_with and ends_with answer yes or no; index_of says where; replace swaps parts.
fn main() {
const s = "kavach.io"
print(s.contains("vach"))
print(s.starts_with("kavach"))
print(s.ends_with(".io"))
}The program ran and printed:
kavach run search.kav
truetruetrue
fn main() {
const s = "the cat sat"
print(s.index_of("cat") ?? -1)
print(s.index_of("dog") ?? -1)
print(s.replace("cat", "dog"))
}The program ran and printed:
kavach run index.kav
4-1the dog sat
index_of gives none when the part isn't there, so ?? -1 picks what to use then.