Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meaning of 2nd parameter in StringOps.split(String, Int)

Tags:

scala

I was trying to split a string and keep the empty strings. Fortunately i found a promising solution which gave me my expected results as following REPL session depicts:

scala> val test = ";;".split(";",-1)
test: Array[String] = Array("", "", "")

I was curious what the second parameter actually does and dived into the scala documentation but found nothing except this:

enter image description here

Also inside the REPL interpreter i only get the following information:

scala> "asdf".split

TAB

def split(String): Array[String]        
def split(String, Int): Array[String]

Question Does anybody have an alternate source of documentation for such badly documented parameters? Or can someone explain what this 2dn parameter does on this specific function?

like image 642
isaias-b Avatar asked Jul 11 '14 13:07

isaias-b


1 Answers

This is the same split from java.lang.String, which as it so happens, has better documentation:

The limit parameter controls the number of times the pattern is applied and therefore affects the length of the resulting array. If the limit n is greater than zero then the pattern will be applied at most n - 1 times, the array's length will be no greater than n, and the array's last entry will contain all input beyond the last matched delimiter. If n is non-positive then the pattern will be applied as many times as possible and the array can have any length. If n is zero then the pattern will be applied as many times as possible, the array can have any length, and trailing empty strings will be discarded.

like image 121
Michael Zajac Avatar answered Oct 28 '22 13:10

Michael Zajac