Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

kotlin
communitycreator

What is String.length in Kotlin?

Hammad Nasir

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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")
}

RELATED TAGS

kotlin
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring