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)
java中split的用法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, "", "", "")
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论