Given the following string...
"localhost:9000/one/two/three"
I want to truncate it after the word two
and get
"localhost:9000/one/two"
I've implemented the methods truncateBefore
and truncateAfter
like this:
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 a regex?
One option using regex:
val beforeAfter = "(^.*two)(.*)$".r
scala> val beforeAfter(after, before) = "localhost:9000/one/two/three"
after: String = localhost:9000/one/two
before: String = /three
Another option using split:
scala> "localhost:9000/one/two/three" split ("two")
res0: Array[java.lang.String] = Array(localhost:9000/one/, /three)
These are not super robust solutions in case you don't have a word two
in the input, but you can handle it accordingly...
One more using regex in for comprehension:
scala> val beforeAfter = "(^.*two)(.*)$".r
beforeAfter: scala.util.matching.Regex = (^.*two)(.*)$
scala> (for {
| matches <- beforeAfter.findAllIn("localhost:9000/one/two/three").matchData
| tokens <- matches.subgroups
| } yield tokens).toList
res0: List[String] = List(localhost:9000/one/two, /three)
which is safe if no matches found:
scala> (for {
| match <- beforeAfter.findAllIn("localhost").matchData
| token <- match.subgroups
| } yield token).toList
res1: List[String] = List()
Splitting after the first literal, without much regex-fu (pun intended).
scala> implicit class `split after`(val s: String) {
| def splitAfter(p: String): (String, String) = {
| val r = (Regex quote p).r
| r findFirstMatchIn s map (m => (s.substring(0, m.end), m.after.toString)) getOrElse (s, "")
| }}
defined class split$u0020after
scala> "abcfoodeffooghi" splitAfter "foo"
res2: (String, String) = (abcfoo,deffooghi)
scala> "abc*def" splitAfter "*"
res3: (String, String) = (abc*,def)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With