How to replace multiple consecutive question marks (?) with a single question mark in Kotlin

1 Answer

0 votes
fun main() {
    val str = "What??? Why?? How?????"

    // Define regex pattern to match one or more '?'
    val pattern = Regex("\\?+")

    // Replace all matches with a single '?'
    val result = pattern.replace(str, "?")

    println(result)
}


 
  
/*
run:
 
What? Why? How?

*/

 



answered Jul 24, 2025 by avibootz
...