How to remove all letters from an alphanumeric string in Kotlin

1 Answer

0 votes
/**
 * Removes all alphabetic characters (letters) from a given string,
 * leaving numbers, symbols, and whitespace intact.
 * 
 * This approach uses [Char.isLetter], which provides full Unicode support
 * (handling accented characters, Cyrillic, etc., out of the box).
 *
 * @param input The alphanumeric string to process.
 * @return A new string with all letters removed.
 */
fun removeLetters(input: String): String {
    // filterNot keeps only the characters that are NOT letters
    return input.filterNot { it.isLetter() }
}

fun main() {
    val mixedString = "abc123XYZ456programming789"
    
    val result = removeLetters(mixedString)
    
    println(result) 
}


/*
run:

123456789

*/

 



answered 5 hours ago by avibootz
...