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


Free Spring Boot Tutorial - 5 Hours Full Course


Watch this course on YouTube at Spring Boot Tutorial | Fee 5 Hours Full Course