How to Find Kotlin String Length

Programmers use strings as a fundamental building block to alter and display text. Knowing how to work with strings is essential in Kotlin, a popular programming language for Android app development and beyond.

The measurement of a string’s length is a crucial component of String manipulation. To help you learn this crucial ability, we’ll go over how to get the length of a string in Kotlin and offer real-world examples.

The length Property

In Kotlin, obtaining the length of a string is straightforward. The String class has a built-in property called length that returns the number of characters in the string. Let’s take a look at how you can use it:

fun main() {
    val myString = "Hello, Kotlin!"
    val length = myString.length

    println("The length of the string is $length")

In this example, we define a string myString containing the text “Hello, Kotlin!” and use the length property to determine its length. The result will be The length of the string is 13.

Handling Multilingual Text

Kotlin is a flexible language that is not just used for writing in English. It can handle strings that contain non-Latin characters and those that are in many different languages.

Regardless of the language or script used, the length attribute determines the length based on the amount of characters. For Example:

fun main() {
    val russianString = "Привет, мир!" // Hello, world! in Russian
    val length = russianString.length

    println("The length of the string is $length")

In this case, the length will be “The length of the string is 12”, as there are 12 characters in the Russian string.

Handling Unicode Characters

Kotlin also handles Unicode characters seamlessly. Each Unicode character, including emojis and special symbols, is counted as a single character by the length property:

fun main() {
    val emojiString = "😀🚀🌟"
    val length = emojiString.length

    println("The length of the string is $length")

The length of the emojiString in this example is "The length of the string is 3", even though it contains three emoji characters.

Handling Empty Strings

When dealing with empty strings, the length property returns 0:

fun main() {
    val emptyString = ""
    val length = emptyString.length

    println("The length of the string is $length")

In this case, the length will be “The length of the string is 0”.


Understanding how to determine the length of a string is a fundamental skill in Kotlin programming. The length property of the String class provides a simple and reliable way to achieve this. Whether you’re working with text in different languages, containing Unicode characters, or handling empty strings, Kotlin’s length property makes it easy to count characters accurately.

Mastering this skill will help you manipulate and format text effectively in your Kotlin applications, whether you’re developing Android apps, web applications, or any other software that involves text processing.

So, go ahead and use the length property to conquer string length challenges in your Kotlin projects!

Related Posts

Define String Constants in Kotlin

Creating an Empty String in Kotlin

Kotlin – Initialize String

Kotlin String Operations with Example

Kotlin – Create Custom Exception

Kotlin Throw Exceptions Handling

Leave a Comment