← Back to lessons

11 Strings and Runes

Intermediate

Strings in Cangjie are arrays of UInt8 underneath. Runes can represent any character in Unicode set and are converted from UInt8.

Type inference from string literal.

String has a instance variable size, which is equal to number of Runes in the string.

We can access individual indexes of the string using [] notation. Indexing is from 0.

We can also do extended loop over elements of string, where i becomes the value at each Index.

Rune literals are preceded with an r, and enclosed with single quatation marks.

strings_and_runes.cj
main() {
    let s = "hello"

    println("Len: ${s.size}")

    for (i in 0..s.size) {
        print("${s[i]} ")
    }

    println()

    for (i in s) {
        println("${i}: ${Rune(i)}")
    }

    let c: Rune = r'T'
    println(c)
}

// Output
// Len: 5
// 104 101 108 108 111 
// 104: h
// 101: e
// 108: l
// 108: l
// 111: o
// T