加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-16 18:56:52 所属栏目:安全 来源:网络整理
导读:我试图拆分一个字符串并保留空字符串.幸运的是,我找到了一个很有前景的解决方案,它给了我预期的结果,如下面的REPL会话描 scala val test = ";;".split(";",-1)test: Array[String] = Array("","","") 我很好奇第二个参数实际上做了什么并潜入scala documenta
我试图拆分一个字符串并保留空字符串.幸运的是,我找到了一个很有前景的解决方案,它给了我预期的结果,如下面的REPL会话描

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参数对这个特定功能的作用吗?

解决方法

这是与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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读