How to move the first word to the end of a string in Kotlin

1 Answer

0 votes
fun move_first_word_to_end_of_string(s: String): String {
    val parts = s.trim().split(Regex("\\s+"))
    if (parts.size <= 1) return s.trim()

    val first = parts.first()
    val rest = parts.drop(1)

    return (rest + first).joinToString(" ")
}

fun main() {
    val s = "Would you like to know more? (Explore and learn)"
    val result = move_first_word_to_end_of_string(s)

    println(result)
}



/*
run:

you like to know more? (Explore and learn) Would

*/

 



answered Feb 5 by avibootz
...