Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove all the whitespaces from a string in Kotlin?

Tags:

kotlin

I've already tried trim() but it only removes whitespaces before and after the text. I want something like this.

var str= "This is an example text"

output:

Thisisanexampletext

like image 642
Kali Avatar asked Feb 02 '20 16:02

Kali


People also ask

How do I remove Whitespaces from a string?

Use the String. replace() method to remove all whitespace from a string, e.g. str. replace(/\s/g, '') . The replace() method will remove all whitespace characters by replacing them with an empty string.

How do you trim whitespace in Kotlin?

Using trim() function Since the string is immutable in Kotlin, it returns a new string having leading and trailing whitespace removed. To just remove the leading whitespaces, use the trimStart() function. Similarly, use the trimEnd() function to remove the trailing whitespaces.

What does trim () do in Kotlin?

Show activity on this post. trim in kotlin remove space int leading and trailing, but when android studio convert java code to kotlin ,convert trim() in java to trim{it <= ' '} in kotlin when change this to trim,It made no difference.


1 Answers

Try

var str = "This is an example text".filter { !it.isWhitespace() }
like image 81
dimsuz Avatar answered Oct 24 '22 20:10

dimsuz