How to split a string on multiple multi‑character delimiters (and keep them) in Scala

1 Answer

0 votes
def splitAndKeep(text: String, delims: Set[Char]): List[String] = {
  if (text.isEmpty) return Nil

  val buf = scala.collection.mutable.ListBuffer[String]()
  var start = 0

  for (i <- 1 until text.length) {
    val prev = text(i - 1)
    val curr = text(i)

    val prevIsDelim = delims.contains(prev)
    val currIsDelim = delims.contains(curr)

    val shouldSplit =
      (prevIsDelim != currIsDelim) ||            // text ↔ delim
      (prevIsDelim && currIsDelim && prev != curr) // delim type changed

    if (shouldSplit) {
      buf += text.substring(start, i)
      start = i
    }
  }

  // Add final segment
  buf += text.substring(start)

  buf.toList
}

val s = "aa==bbb---cccc++++ddddd"
val delimiters = Set('=', '-', '+')

println(splitAndKeep(s, delimiters))




/*
run:

List(aa, ==, bbb, ---, cccc, ++++, ddddd)

*/

 



answered Mar 10 by avibootz

Related questions

...