Simon Lin
09/10/2021, 8:43 AMval text = "aas211w"
if (text.any { it.isDigit() } && text.any { it.isLetter() }) {
}
ephemient
09/10/2021, 8:56 AM"ļ¼³".any { it.isLetter() } == true
"š ".any { it.isLetter() } == false
Tobias Suchalla
09/10/2021, 10:21 AMany
also only checks if at least one of the chars matches the predicate, I suspect you might want all
:
val text = "aas211w"
if (text.all { it.isDigit() || it.isLetter() }) {
// do something
}
Matteo Mirk
09/10/2021, 12:58 PMval text = """aas211Wš ļ¼³""" // contains UTF8 letters
text.matches("""[\p{L}\d]+""".toRegex()) // true, UTF8-inclusive
"""[a-zA-Z\d]+"""
Klitos Kyriacou
09/10/2021, 1:42 PMMatteo Mirk
09/10/2021, 2:34 PMbrandonmcansh
09/11/2021, 6:10 PMisLetterOrDigit()
in the JVM already?Matteo Mirk
09/13/2021, 1:53 PMfunĀ Char.isLetterOrDigit():Ā Boolean
an yes, there are the static methods in the Char class
⢠public static boolean isLetterOrDigit(char ch)
since Java 1
⢠public static boolean isLetterOrDigit(int codePoint)
since Java 5
good catch Brandon, I totally forgot those!ephemient
09/14/2021, 5:49 PM.isLetterOrDigit()
doesn't help, but the original can be modified to be Unicode-safe on JVM:
"aas211w".codePoints()
.map { if (Character.isDigit(it)) 1 else if (Character.isletter(it)) 2 else 0 }
.reduce(0, Int::or) == 3
generateSequence(0) { offsetByCodePoints(it, 1) }.takeWhile { it < length }.map { codePointAt(it) }
instead of .codePoints()