object RemoveTheLastOccurrenceOfAWordFromAString_Scala {
def removeLastOccurrenceOfAWordFromAString(str: String, word: String): String = {
// Find the position of the last occurrence of word in str
val pos = str.lastIndexOf(word)
if (pos != -1) {
str.substring(0, pos) + str.substring(pos + word.length)
} else {
str
}
}
def main(args: Array[String]): Unit = {
var str = "scala c# c python scala c++ java scala rust"
val word = "scala"
str = removeLastOccurrenceOfAWordFromAString(str, word)
println(str)
}
}
/*
run:
scala c# c python scala c++ java rust
*/