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

extract digit numbers from string in kotlin

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

81aabf9d-844e-4086-9621-f3afed02e163

to

81984440869621302163

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

>Solution :

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()
}
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