What is String.length in Kotlin?

String.length is a property in Kotlin that is used to find the length of a string.

Visual representation of the `length` method in Kotlin

Syntax

String.length

Return value

The function returns an integer value, i.e., the number of characters in String.

Example

Let’s look at an example. In the code below, we create two strings, str1 and str2. We then use the length function in line 5 and line 11 to display the lengths of the strings.

fun main(args: Array<String>) {
// Example 1
val str1 = "Educative"
print("The length of " + str1 + " is ")
print(str1.length)
print("\n")
// Example 2
val str2 = "Edpresso"
print("The length of " + str2 + " is ")
print(str2.length)
print("\n")
}

Free Resources