How to remove all occurrences of a word from a string in Kotlin

2 Answers

0 votes
fun main() {
    var s = "Kotlin makes coding concise Kotlin cross-platform Kotlin fun"
    val remove = "Kotlin"

    s = s.replace(remove, "")

    println(s)
}
 

 
/*
run:

 makes coding concise  cross-platform  fun
 
*/

 



answered Dec 24, 2024 by avibootz
0 votes
fun main() {
    var s = "Kotlin makes coding concise Kotlin cross-platform Kotlin fun"
    val remove = "Kotlin"

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

    println(s)
}
 

 
/*
run:

makes coding concise cross-platform fun
 
*/

 



answered Dec 24, 2024 by avibootz
...