Kotlin – String Length

Get String Length in Kotlin

To get the length of a given string in Kotlin, you can read the length read-only property of the string object.

str.length

This expression returns the length of the string str as an integer.

Examples for String.length

1. Find length of the string “Hello World”

In the following program, we take a string value in str, find its length, and print the length to output.

Kotlin Program

fun main() {
    val str = "Hello World"
    val len = str.length
    println(len)
}

Output

11