How to truncate a string after a word in scala

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?

+5
source share
5 answers

Separation after the first literal without much regular expression (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)
+2
source

One parameter 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)

, two , ...

:

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)

, :

scala> (for {
     |   match <- beforeAfter.findAllIn("localhost").matchData
     |   token <- match.subgroups
     |  } yield token).toList
res1: List[String] = List()
+6

, , .

scala> val url = "localhost:9000/one/two/three"
url: String = localhost:9000/one/two/three

scala> url.reverse.dropWhile(_ != '/').reverse
res0: String = localhost:9000/one/two/

:

scala> url.drop(url.reverse.indexOf('/'))
res1: String = host:9000/one/two/three
+1

, ... :

package object typeExtensions {

  implicit class StringExtensions(val string: String) extends AnyVal {

    def truncateAfter(pattern: String) = beforeAfter(pattern).map(_._1).getOrElse(string)
    def truncateBefore(pattern: String) = beforeAfter(pattern).map(_._2).getOrElse(string)

    private def beforeAfter(pattern: String) = {
      if (string.contains(pattern)) {
        val beforeAfter = ("(^.*" + Pattern.quote(pattern) + ")(.*)$").r
        val beforeAfter(before, after) = string
        Some(before, after)
      } else None
    }
  }
}

; -)

+1

Scala 2.13, , String, :

"localhost:9000/one/two/three" match {
  case s"${prefix}two${suffix}" => s"${prefix}two"
  case _                        => "no match"
}
// String = "localhost:9000/one/two"
0

Source: https://habr.com/ru/post/1529470/


All Articles