Kotlin Remove All Whitespaces from a String Example

This Kotlin example shows how to remove all whitespaces in a given string using regular expressions in Kotlin.

Program to Remove All Whitespaces

package net.javaguides.kotlin.examples

fun main(args: Array <String> ) {

    var sentence = " Source Code Examples . Net     "

    sentence = sentence.replace("\\s".toRegex(), "")

    println("After replacement: $sentence")
}
Output:
After replacement: SourceCodeExamples.Net



Comments