Search code examples
stringscala

how to remove the last part of a string based on a condition in scala


i have a string

"108.0.5359.124"

i want to remove the string's last part from the dot like this "108.0.5359" i don't want to hard code Istrin just want the string before the last dot every time whatever the string is the format will remain same


Solution

  • You can use split, then dropRight(1) (drop the last elem) and mkString back to string:

    val s = "108.0.5359.124"
    val ss = s.split('.').dropRight(1).mkString(".")
    
    println(ss) //108.0.5359