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")
}
New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources