Given the following line ...
"localhost:9000/one/two/three"
I want to trim it after a word twoand get
"localhost:9000/one/two"
I implemented the methods truncateBeforeand truncateAfterin the following way:
def truncateBefore(s: String, p: String) = {
s.substring(s.indexOf(p) + p.length, s.length)
}
def truncateAfter(s: String, p: String) = {
s.substring(0, s.indexOf(p) + p.length)
}
These methods work and return the expected results:
scala> truncateAfter("localhost:9000/one/two/three", "two")
res1: String = "localhost:9000/one/two"
scala> truncateBefore("localhost:9000/one/two/three", "two")
res2: String = "/three"
Is there a better way to do this in Scala? Preferably with regex?
source
share