本文介绍了拆分字符串时如何避免删除尾随空项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在做:
"b::::c:::".split(':')
结果:
["b", "", "", "", "c", "", ""] # expect
["b", "", "", "", "c"] # actual
这里有什么问题?我怎样才能得到我所期望的.
What is the problem here? how can i get what i expected.
推荐答案
.split(pattern=$;, [limit])
有一个 limit
参数.如果省略 limit
,则抑制尾随空字段.您需要提供一个负 limit
There's a limit
parameter to .split(pattern=$;, [limit])
. If limit
is omitted, trailing null fields are suppressed. You need to provide a negative limit
"b::::c:::".split(':', -1)
但请记住,这将在数组末尾返回三个 ""
值.
but bear in mind that this will return three ""
values at the end of the array.
result: ["b", "", "", "", "c", "", "", ""]
这篇关于拆分字符串时如何避免删除尾随空项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!