scala has a standard way of splitting a string in StringOps.split
this behavior surprised me somewhat.
To demonstrate using the quick convenience feature
def sp(str: String) = str.split('.').toList
the following expressions evaluate to true
(sp("") == List("")) //expected (sp(".") == List()) //I would have expected List("", "") (sp("ab") == List("a", "b")) //expected (sp(".b") == List("", "b")) //expected (sp("a.") == List("a")) //I would have expected List("a", "") (sp("..") == List()) // I would have expected List("", "", "") (sp(".a.") == List("", "a")) // I would have expected List("", "a", "")
so I expected split to return an array with (number of separator occurrences) + 1 element, but this is clearly not the case.
This is almost higher, but delete all trailing blank lines, but this is not true for splitting blank lines.
I can not define the template here. What rules does StringOps.split follow?
For bonus points, is there a good way (without copying / adding lines too much) to get the split I expect?
source share