Home > Mobile >  Remove character or word from string in kotlin
Remove character or word from string in kotlin

Time:01-25

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

For example

val string = "Hey 123"

or

val string = "Hey How are you 123"

or

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

and output

string = 123

CodePudding user response:

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() }
  •  Tags:  
  • Related