本文介绍了StringOps.split(String, Int) 中第二个参数的含义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图拆分一个字符串并保留空字符串.幸运的是,我找到了一个有前途的解决方案,它给了我预期的结果,如下 REPL 会话所示:

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

我很好奇第二个参数实际上做了什么,并深入到 :

limit 参数控制模式出现的次数应用并因此影响结果数组的长度.如果限制 n 大于零,则模式将应用于最多 n - 1 次,数组的长度将不大于 n,并且数组的最后一个条目将包含最后一个匹配项之外的所有输入分隔符.如果 n 为非正数,则该模式将应用为尽可能多次,数组可以有任何长度.如果 n 为零然后模式将被应用尽可能多的次数,数组可以有任意长度,尾随空字符串将被丢弃.

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:

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

scala> "asdf".split
def split(String): Array[String]
def split(String, Int): Array[String]

QuestionDoes 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?

解决方案

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

这篇关于StringOps.split(String, Int) 中第二个参数的含义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 13:46