在 Scala 中拆分逗号分隔的字符串:缺少尾随空字符串? [英] Splitting a Comma-Separated String in Scala: Missing Trailing Empty Strings?

查看:51
本文介绍了在 Scala 中拆分逗号分隔的字符串:缺少尾随空字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 csv 格式的数据文件.我正在尝试使用基本拆分命令 line.split(',') 拆分每一行但是当我得到这样的字符串 "2,,",而不是像这样返回一个数组 Array(2,"","")我只是得到一个 Array: Array(2).我肯定遗漏了一些基本的东西,有人可以帮助指出在此处拆分逗号分隔字符串的正确方法吗?

I have a data file in csv format. I am trying to split each line using the basic split command line.split(',') But when I get a string like this "2,,", instead of returning an array as thus Array(2,"","") I just get an Array: Array(2). I am most definitely missing something basic, could someone help point out the correct way to split a comma separated string here?

推荐答案

这是从 Java 继承的.您可以通过使用 split(String regex, int limit) 重载来实现您想要的行为:

This is inherited from Java. You can achieve behavior you want by using the split(String regex, int limit) overload:

"2,,".split(",", -1) // = Array(2, "", "")

注意 String 而不是 Char.

正如 Java Docs 所解释的,limit 参数的用法如下:

As explained by the Java Docs, the limit parameter is used as follows:

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

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.

来源

使用 split(separator: Char) 将调用上面的重载,使用 limit 为零.

Using split(separator: Char) will call the overload above, using a limit of zero.

这篇关于在 Scala 中拆分逗号分隔的字符串:缺少尾随空字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆