To get the length of a given string in Kotlin language, you can use length
property of the String object.
In this example,
str1
with an string value of "Hello World"
.length
property of the string object str1
, and store the returned value in length
variable.String.length
property returns the length of this string.length
to standard output.fun main(args: Array<String>) {
val str1 = "Hello World";
val length = str1.length
print("Length of given string is: $length");
}
Length of given string is: 11
In this example,
"apple"
in variable named str2
.length
property of the string object str2
, and store the returned value in length
variable.String.length
property returns the length of this string.length
to standard output.fun main(args: Array<String>) {
val str2 = "apple";
val length = str2.length
print("Length of given string is: $length");
}
Length of given string is: 5
In this tutorial, we learned How to get Length of String in Kotlin language with well detailed examples.