views:

231

answers:

1

Im looking for an elegant way in Scala to split a given string into substrings of fixed size (the last string in the sequence might be shorter).

So

split("Thequickbrownfoxjumps", 4)

should yield

["Theq","uick","brow","nfox","jump","s"]

Of course I could simply use a loop but there has to be a more elegant (functional style) solution.

+18  A: 
scala> val grouped = "thequickbrownfoxjumps".grouped(4).toList
grouped: List[String] = List(theq, uick, brow, nfox, jump, s)
michael.kebe
I guess there will be no shorter solution :) Thanks a lot!
MartinStettner