Hey I want to remove text from string for example ...
# android
v
Hey I want to remove text from string for example
val string = "Hey 123"
or
val string = "Hey How are you 123"
and output
string = 123
stackoverflow 3
j
string.replace(..., "")
you can combine multiple
substringAfter/Before
too but passing a regex to the replace is simpler (or easier to read)
v
Copy code
string.replace("""\D++""".toRegex(), "")
v
thanks a milllion
p
Or pure kotlin: filter { it.isDigit }.toInt()
v
thanks @Paul Woitaschek
v
string.filter { it.isDigit() }
actually, but yeah
v
okk
c
You can do this with idiomatic kotlin by creating an extension function.
p
That's a lot of code. You forgot ä
Also there is àā. And add all the special characters. Oh and Unicode 🦄
😂 3
j
I swear I read
string = "hey 123"
, if it has only digit, then the
string.filter(String::isDigit)
is more readable