1. 程式人生 > >scala之split()函數用法

scala之split()函數用法

string cal ring array 空字符 spl 可能 style sca

split()函數:

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

scala> "a-b-c-d-e".split("-",3)
res0: Array[String] = Array(a, b, c-d-e)

scala> "a-b-c-d-e".split("-",4)
res1: Array[String] = Array(a, b, c, d-e)

scala> "a-b-c-d-e".split("-",5)
res2: Array[String] = Array(a, b, c, d, e)

scala> "a-b-c-d-e".split("-",6)
res3: Array[String] = Array(a, b, c, d, e)


scala> "a-b-c-d-e".split("-")
res6: Array[String] = Array(a, b, c, d, e)


scala> "a-b-c-d--e---".split("-")
res7: Array[String] = Array(a, b, c, d, "", e)

//arg1=0,split函數盡可能多的匹配arg0,不保留處於末尾位置的空字符串,空字符串例外。比如:
scala> "--a-b-c-d--e---".split("-")
res8: Array[String] = Array("", "", a, b, c, d, "", e)
scala> "".split("-",0)
res1: Array[String] = Array("")


//arg1<0,保留末尾的空字符串
scala> "--a-b-c-d--e---".split("-",-1)
res0: Array[String] = Array("", "", a, b, c, d, "", e, "", "", "")





scala之split()函數用法