split
split
过滤器通过给定的分隔符分割字符串,并返回字符串列表
1 2
{% set items = "one,two,three"|split(',') %}
{# items contains ['one', 'two', 'three'] #}
您还可以传递一个 limit
参数
- 如果
limit
为正数,则返回的序列最多包含 limit 个元素,最后一个元素包含字符串的其余部分; - 如果
limit
为负数,则返回除最后 -limit 个元素之外的所有组件; - 如果
limit
为零,则将其视为 1。
1 2
{% set items = "one,two,three,four,five"|split(',', 3) %}
{# items contains ['one', 'two', 'three,four,five'] #}
如果 delimiter
是空字符串,则 value 将按相等的块分割。长度由 limit
参数设置(默认为一个字符)。
1 2 3 4 5
{% set items = "123"|split('') %}
{# items contains ['1', '2', '3'] #}
{% set items = "aabbcc"|split('', 2) %}
{# items contains ['aa', 'bb', 'cc'] #}
参数
delimiter
: 分隔符limit
: limit 参数