https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
n

Nick Halase

02/10/2020, 12:57 AM
anyone know of a good way to check if a String contains ONLY ASCII characters in an
ios
target
actual
?
aside from comparing the int value of each character to the ascii charset of utf-8
i

ian.shaun.thomas

02/10/2020, 1:49 AM
You could get clever and write it to a byte array then see if the int values are the same at each character.
n

Nick Halase

02/10/2020, 1:49 AM
Yeah, that's what I ended up doing. I haven't tested it yet, though
Copy code
actual fun String.isAscii(): Boolean = this.toCharArray().any { c -> c.toInt() !in 0..127 }
i

ian.shaun.thomas

02/10/2020, 6:34 AM
Well that ends up being a lot of searching. I mean take the char array and walk it casting each char to a byte and check if the int values of each match
3 Views