Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Remove character or word from string in kotlin

Hey I want to remove character/word or sentence from string.

For example

val string = "Hey 123"

or

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

val string = "Hey How are you 123"

or

val string = "Hey!! How are you 123"

and output

string = 123

>Solution :

If you only want the digits:

val result = string.filter { it.isDigit() }

Alternatively if you want to omit letters (and maybe also whitespace):

val result = string.filter { !it.isLetter() }
val result = string.filter { !it.isLetter() && !it.isWhitespace() }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading