ADVERTISEMENT
ADVERTISEMENT

Home » Kotlin » Kotlin programs » Kotlin string programs

Kotlin program to remove all whitespaces from the string

Kotlin | Removing whitespaces from the string: Here, we are going to learn how to remove all whitespaces from the given string in Kotlin programming language?
Submitted by IncludeHelp, on April 29, 2020

Given a string, we have to remove all whitespaces from it.

Example:

    Input:
    string = "Hello world!"

    Output:
    "Helloworld!"

Program to remove all whitespaces from the string in Kotlin

In this program, at first, we are reading a string and them replacing all whitespaces using the replace() function.

package com.includehelp.basic

import java.util.*

//Main Function, entry Point of Program
fun main(args: Array<String>) {
    // InputStream to get Input
    val scanner = Scanner(System.`in`)

    //Input String
    print("Enter String : ")
    var str = scanner.nextLine()

    //Replace character with Empty String
    val newStr=str.replace("\\s".toRegex(),"")

    //Print String after Replacement
    println("String after Removed WhiteSpaces : $newStr ")
}

Output

Run 1:
Enter String : Hello includehelp how are you ?
String after Removed WhiteSpaces : Helloincludehelphowareyou?
---
Run 2:
Enter String : Covid -19 [pandimic spread all over the world
String after Removed WhiteSpaces : Covid-19[pandimicspreadallovertheworld
ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.