2

I am trying to sanitize a QR code output. The value I am trying to send via Retrofit is

010868193700666621762185642311216939172106131020190603

but OKHttp log show

example.com/endpoint/etc/&Qrcode=%1D010868193700666621762185642311216939%1D172106131020190603

When I use this .trim().replace("\u00D", "")

example.com/endpoint/etc/&Qrcode=010868193700666621762185642311216939%1D172106131020190603

How do I remove those unwanted characters?

LutfiTekin
  • 441
  • 1
  • 10
  • 19
  • You can use Kotlin's String.filter Reference : https://stackoverflow.com/questions/58812220/extract-numbers-from-a-string-kotlin – Franz Andel Jul 03 '20 at 09:17

2 Answers2

1

https://howtodoinjava.com/regex/java-clean-ascii-text-non-printable-chars/ This here solved my problem. I converted it to an extension in Kotlin like this

val String.cleanTextContent: String
    get() {
        // strips off all non-ASCII characters
        var text = this
        text = text.replace("[^\\x00-\\x7F]".toRegex(), "")

        // erases all the ASCII control characters
        text = text.replace("[\\p{Cntrl}&&[^\r\n\t]]".toRegex(), "")

        // removes non-printable characters from Unicode
        text = text.replace("\\p{C}".toRegex(), "")
        return text.trim()
    }
LutfiTekin
  • 441
  • 1
  • 10
  • 19
0

You can try below to collect only digits from the string.

var result = string.filter { it.isDigit() }
Shalu T D
  • 3,921
  • 2
  • 26
  • 37