Kotlin String - length



The Kotlin string length is used to return the length of the character sequence from the specified string.

In Kotlin, length is a read-only property of the String class, providing an easy way to get the number of characters in any string object.

Syntax

Following is the syntax of the Kotlin string length property −

val length: Int

Parameters

This is a property does not accepts any parameters.

Return value

It returns the length of the string sequence.

Example 1

Following is the basic example, to display the length of the string −

fun main(){
   val myString = "this is kotlin string"
   val length  = myString.length;
   println(length)
}

Output

Following is the output −

21

Example 2

Now, let's see another example. Here, we display the string if the string length is greater than 10 −

fun main(){
   val myString = "this is kotlin string"
   val length  = myString.length;
   if(length > 10){
      println(myString)
   }
   else{
      println("string length is smaller than 10")
   }
}

Output

Following is the output −

this is kotlin string
kotlin_strings.htm
Advertisements