To replace multiple spaces with a single space in Kotlin, you can use regular expressions and the `replace` function.
In this example,
str
with multiple spaces.replace
function with a regular expression \s+
to replace multiple spaces with a single space.str
.fun main() {
val str = "This is a test string"
val modifiedStr = str.replace(Regex("\\s+"), " ")
println(modifiedStr)
}
This is a test string
In this tutorial, we learned How to Replace multiple Spaces with a Single Space in String in Kotlin language with well detailed examples.