How to get the second word of a string in Scala

1 Answer

0 votes
val sentence = "c scala rust java c# python c++";

val words = sentence.split(" ")

val secondWord = if (words.length > 1) words(1) else "No second word"

println(secondWord)



/*
run:

scala

*/

 



answered Oct 3, 2024 by avibootz
...