How to move all special characters to the beginning of a string in Kotlin

1 Answer

0 votes
fun moveSpecialCharactersToBeginning(s: String): String {
    val (specials, chars) = s.partition { !(it.isLetterOrDigit() || it.isWhitespace()) }
    
    return specials + chars
}

fun main() {
    val s = "c++23@c&^java*(rust) php <>/python 3.14.2"
    
    println(moveSpecialCharactersToBeginning(s))
}



/*
run:

++@&^*()<>/..c23cjavarust php python 3142

*/

 



answered Dec 12, 2025 by avibootz

Related questions

...