How to replace duplicate spaces in a string with Kotlin

1 Answer

0 votes
fun main() {
    var s = "  Kotlin    makes   coding concise"

    s = s.replace("\\s+".toRegex(), " ")
    s = s.trim()

    println(s)
}
 

 
/*
run:

Kotlin makes coding concise
 
*/

 



answered Dec 24, 2024 by avibootz
...