
- Kotlin - Home
- Kotlin - Overview
- Kotlin - Environment Setup
- Kotlin - Architecture
- Kotlin - Basic Syntax
- Kotlin - Comments
- Kotlin - Keywords
- Kotlin - Variables
- Kotlin - Data Types
- Kotlin - Operators
- Kotlin - Booleans
- Kotlin - Strings
- Kotlin - Arrays
- Kotlin - Ranges
- Kotlin - Functions
- Kotlin Control Flow
- Kotlin - Control Flow
- Kotlin - if...Else Expression
- Kotlin - When Expression
- Kotlin - For Loop
- Kotlin - While Loop
- Kotlin - Break and Continue
- Kotlin Collections
- Kotlin - Collections
- Kotlin - Lists
- Kotlin - Sets
- Kotlin - Maps
- Kotlin Objects and Classes
- Kotlin - Class and Objects
- Kotlin - Constructors
- Kotlin - Inheritance
- Kotlin - Abstract Classes
- Kotlin - Interface
- Kotlin - Visibility Control
- Kotlin - Extension
- Kotlin - Data Classes
- Kotlin - Sealed Class
- Kotlin - Generics
- Kotlin - Delegation
- Kotlin - Destructuring Declarations
- Kotlin - Exception Handling
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