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

1 Answer

0 votes
def move_first_word_to_end_of_string(s: String): String = {
  val parts = s.trim.split("\\s+").toList

  parts match {
    case Nil | _ :: Nil => s.trim
    case first :: rest  => (rest :+ first).mkString(" ")
  }
}

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
...