How to calculate string length without spaces in Kotlin

1 Answer

0 votes
fun lengthWithoutSpaces(str: String): Int {
    return str.replace(" ", "").length
}

fun main() {
    val str = "Kotlin Programming Language"
    
    val length = lengthWithoutSpaces(str)
    
    println("Length without spaces: $length")
}


 
/*
run:

Length without spaces: 25
 
*/

 



answered Jan 11, 2025 by avibootz
...