gpt4 book ai didi

string - Scala 拆分行为 : consecutive occurrence of split pattern

转载 作者:行者123 更新时间:2023-12-01 09:24:07 25 4
gpt4 key购买 nike

使用 Scala 的标准 String 库:

"a,,,".split(",")

Array(a) ,而不是

数组(a, "", "", "")

正如人们所期望的那样。有什么办法可以强制吗?

"a,,,b".split(",")

Array(a, "", "", "", b) 这很好。

如果这是一个错误,我会感到惊讶,那么有人理解这种行为背后的所谓逻辑吗?

最佳答案

documentation split 方法我们可以阅读:

Trailing empty strings are therefore not included in the resulting array.

但是您可以使用带有附加 limit 参数的 split 方法。例如:

"a,,,".split( "," ,-1)

将负数设置为 limit 参数将导致该模式被应用尽可能多的次数。

here了解详情

关于string - Scala 拆分行为 : consecutive occurrence of split pattern,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28470722/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com