Kotlin String Examples

In Kotlin, the String class represents character strings.

Access characters of a String

Index access operator can be used.

val str = "abCdEf"
str[2] //"C"

Iterate through a String

for (c in str) {

Add leading zeros to a number

Method padStart() of the CharSequence class can add any characters to the beginning of a string/char sequence.

val padWithZeros = "1".padStart(2, '0') 
//output: "01"

Leave a Comment

Your email address will not be published.

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.