Skip to content
Kavachकवच

Strings: Joining

● Beginner2 min

+ joins two strings; join() puts a list of strings together; repeat() repeats one.

fn main() {
    const first = "Asha"
    const last = "Rao"
    print(first + " " + last)
    print(["red", "green", "blue"].join(", "))
    print("ab".repeat(3))
}

The program ran and printed:

kavach run join.kav

Asha Raored, green, blueababab
join.kav: Kavach code, then what running it prints

join works on a list of characters too, and contains takes a Char as well as a String:

fn vowels(word: String) -> Int {
    word.chars().count(c => "aeiou".contains(c))
}

fn main() {
    const word = "banana"
    print(word.chars().reverse().join(""))
    print(vowels(word))
}

The program ran and printed:

kavach run chars-join.kav

ananab3
chars-join.kav: Kavach code, then what running it prints
tested