split
¶
这个 split
筛选器按给定的分隔符拆分字符串并返回字符串列表:
1 2 | {% set foo = "one,two,three"|split(',') %}
{# foo contains ['one', 'two', 'three'] #}
|
你也可以通过 limit
论点:
- 如果
limit
为正,则返回的数组最多包含个限制元素,最后一个元素包含其余的字符串; - 如果
limit
为负,则返回除最后一个限制之外的所有组件; - 如果
limit
为0,则将其视为1。
1 2 | {% set foo = "one,two,three,four,five"|split(',', 3) %}
{# foo contains ['one', 'two', 'three,four,five'] #}
|
如果 delimiter
为空字符串,则值将被相等的块拆分。长度由 limit
参数(默认为一个字符)。
1 2 3 4 5 | {% set foo = "123"|split('') %}
{# foo contains ['1', '2', '3'] #}
{% set bar = "aabbcc"|split('', 2) %}
{# bar contains ['aa', 'bb', 'cc'] #}
|
争论¶
delimiter
:分隔符limit
:limit参数