scala之split()函数用法
Posted fulse
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了scala之split()函数用法相关的知识,希望对你有一定的参考价值。
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()函数用法的主要内容,如果未能解决你的问题,请参考以下文章