scala - StringOps.split(String,Int)中第二个参数的含义

标签 scala

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

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

我很好奇第二个参数实际上是做什么的,并深入到scala documentation中,但除了以下内容外,什么都没有找到:

同样在REPL解释器中,我仅获得以下信息:
scala> "asdf".split

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

问题
有人针对这些记录不良的参数有其他文献来源吗?
还是有人可以解释这个2dn参数对这个特定功能的作用?

最佳答案

这是与split相同的java.lang.String,碰巧它具有更好的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.

关于scala - StringOps.split(String,Int)中第二个参数的含义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24698666/

相关文章:

scala - Spark : Replace Null value in a Nested column

Play Framework 2.1 RC1 中的 AJAX 文件上传传送一个空文件

scala - 我需要更改什么才能使 Scala 2.13 MultiDict 用作​​ 2.12 的 MultiMap 的直接替代品?

postgresql - 使用 Mockito 模拟具有功能参数的 Scala 方法

Scala:编译时间常数

scala - SBT 预计什么时候移植到 Scala 2.8?

Scala:不建议使用 `-` [dash,减号]命令,而建议使用 `onFailure`,并将在0.14.0中将其删除

scala - 在 Scala 中使用元组作为键

serialization - Scala序列化

java - 如何从 Scala 中的辅助构造函数访问字段?