How to split a string with a specific format into an array using a specified delimiter in the Anqi CMS template?
split
The filter can split strings of a specific format into an array using a specified delimiter.If the delimiter does not exist in the string, it will return an array of length 1, with the value being the string itself.If the delimiter is empty, the array will be split by each utf8 character.
Can also be usedmake_list
To quickly cut strings into arrays.make_list
Each character will be split into an array with a value, a letter is one, and a Chinese character is one.
How to use
split
How to use filters:
{{ obj|split:"拼接符" }}
make_list
How to use filters:
{{ obj|make_list }}
For example, it is necessary tosplits, the, string, 安企CMS
according to,
Cut into an array, you can write this:
{{ "splits, the, string, 安企CMS"|split:", "|stringformat:"%#v" }}
# 显示结果
[]string{"splits", "the", "string", "安企CMS"}
split
Filters also support use in assignment labels (sets), storing the judgment results into a variable, such as:
{% set values = "splits, the, string, 安企CMS"|split:", " %}
{% for item in values %}
<span>{{item}}/</span>
{% endfor %}
# 显示结果
splits/ the/ string/ 安企CMS/
Sample Demo
split
Filter
{{ "splits, the, string, 安企CMS"|split:", " }}
{{values|stringformat:"%#v"}}
# 显示结果
[]string{"splits", "the", "string", "安企CMS"}
{{ "Hello, 99, 3.140000, good"|split:", "|join:", " }}
# 显示结果
Hello, 99, 3.140000, good
make_list
Filter
{{ "john doe"|make_list|join:", " }}
{% for char in "john doe"|make_list %}{{ char }}{% endfor %}
# 显示结果
j, o, h, n, , d, o, e
john doe