Home > Software engineering >  How to remove first character in Kotlin
How to remove first character in Kotlin

Time:04-16

If the first letter is 0 I want to remove it. For example: If it is "01" it will be "1" but if it is "10" there will be no change.

CodePudding user response:

You can use drop(1).

if(str[0] == '0'){
   str = str.drop(1)
}

CodePudding user response:

you can do something like this

var str1 = "01abcdefg"
var str2 = "0"

val result = str1.startsWith(str2) 

if( result ) {
    str1 = str1.drop(1)
    println("String starts with specified string.")
    println(str1)
} else {
    println("String does not start with specified string.")
}

see live test https://pl.kotl.in/SyNXcNOkE

  • Related