0

i want to extract numbers from string how can i do that in Kotlin ?

81aabf9d-844e-4086-9621-f3afed02e163

to

81984440869621302163

2 Answers2

4

You can use String filter:

val text= "81aabf9d-844e-4086-9621-f3afed02e163"
val result = text.filter{it.isDigit()}
      
Nitish
  • 3,075
  • 3
  • 13
  • 28
1

just use this extension function

fun String?.extractDigit(): Long {
    if (this == null) {
        return 0
    }
    var nums = this.replace("\\D+".toRegex(), "")
    Log.i("extractDigit", nums)

    if (nums.isEmpty())
        return 0

    return nums.toLong()
}
Arash Jahani
  • 192
  • 6