如何从 Kotlin 中的字符串中删除所有分隔符?

How do I remove all separator characters from a string in Kotlin?

我正在尝试清理二维码输出。 我试图通过 Retrofit 发送的值是

010868193700666621762185642311216939172106131020190603

但是 OKHttp 日志显示

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

当我使用这个 .trim().replace("\u00D", "")

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

如何删除不需要的字符?

您可以在下面尝试仅从字符串中收集数字。

var result = string.filter { it.isDigit() }

https://howtodoinjava.com/regex/java-clean-ascii-text-non-printable-chars/ 这在这里解决了我的问题。 我像这样将它转换为 Kotlin 中的扩展

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