Splitting string into groups

后端 未结 9 968
再見小時候
再見小時候 2020-12-15 09:52

I\'m trying to \'group\' a string into segments, I guess this example would explain it more succintly

scala> val str: String = \"aaaabbcddeeeeeeffg\"
...          


        
相关标签:
9条回答
  • 2020-12-15 10:52

    Seems that all other answers are very concentrated on collection operations. But pure string + regex solution is much simpler:

    str split """(?<=(\w))(?!\1)""" toList
    

    In this regex I use positive lookbehind and negative lookahead for the captured char

    0 讨论(0)
  • 2020-12-15 10:55

    You can split the string recursively with span:

    def s(x : String) : List[String] = if(x.size == 0) Nil else {
        val (l,r) = x.span(_ == x(0))
        l :: s(r) 
    }
    

    Tail recursive:

    @annotation.tailrec def s(x : String, y : List[String] = Nil) : List[String] = {
        if(x.size == 0) y.reverse 
        else {
            val (l,r) = x.span(_ == x(0))
            s(r, l :: y)
        }
    }
    
    0 讨论(0)
  • 2020-12-15 10:56

    A functional* solution using fold:

    def group(s : String) : Seq[String] = {
      s.tail.foldLeft(Seq(s.head.toString)) { case (carry, elem) =>
        if ( carry.last(0) == elem ) {
          carry.init :+ (carry.last + elem)
        }
        else {
          carry :+ elem.toString
        }
      }
    }
    

    There is a lot of cost hidden in all those sequence operations performed on strings (via implicit conversion). I guess the real complexity heavily depends on the kind of Seq strings are converted to.

    (*) Afaik all/most operations in the collection library depend in iterators, an imho inherently unfunctional concept. But the code looks functional, at least.

    0 讨论(0)
提交回复
热议问题