Filter-String(字符串)
Filter-String
String(字符串)
- append
- capitalize
- downcase
- escape
- escape_once
- escape_quotes
- lstrip
- rstrip
- strip
- newline_to_br
- prepend
- remove
- remove_first
- replace
- replace_first
- slice
- split
- strip_html
- strip_newlines
- truncate
- truncatewords
- upcase
append
将给定字符串添加到字符串的末尾。
// 1.数据
{
"product": {
"url": "/products/health-potion"
},
"request": {
"origin": "https://polinas-potent-potions.myfecify.com"
}
}
// 2.代码
{%- assign path = product.url -%}
{{ request.origin | append: path }}
// 3.输出
https://polinas-potent-potions.myfecify.com/products/health-potion
capitalize
将字符串中的第一个单词大写,其余字符小写
// 1.代码
{{ 'this sentence should start with a capitalized word.' | capitalize }}
// 2.输出
This sentence should start with a capitalized word.
downcase
将字符串转换为全部小写字符。
// 1.数据
{
"product": {
"title": "Health potion"
}
}
// 2.代码
{{ product.title | downcase }}
// 3.输出
health potion
escape
转义 HTML 中的特殊字符,例如<>、'和&,并将字符转换为转义序列。
// 1.代码
{{ '<p>Text to be escaped.</p>' | escape }}
// 2.输出
<p>Text to be escaped.</p>
escape_once
转义字符串但不改变已经转义的字符。
# applying the escape_once filter to already escaped text skips characters in HTML entities:
{{ "<p>Text to be escaped.</p>" | escape_once }}
escape_quotes
将字符串里面的单引号'
替换为双引号"
// 1.数据
{
"product": {
"title": "Health 'potion'"
}
}
// 2.代码
{{ product.title | escape_quotes }}
// 3.输出
Health "potion"
lstrip
去除字符串左侧的所有空格。
// 1.代码
{%- assign text = ' Some potions create whitespace. ' -%}
"{{ text }}"
"{{ text | lstrip }}"
// 2.输出
" Some potions create whitespace. "
"Some potions create whitespace. "
rstrip
去除字符串右侧的所有空格。
// 1.代码
{%- assign text = ' Some potions create whitespace. ' -%}
"{{ text }}"
"{{ text | rstrip }}"
// 2.输出
" Some potions create whitespace. "
" Some potions create whitespace."
strip
去除字符串左侧和右侧的所有空格。
// 1.代码
{%- assign text = ' Some potions create whitespace. ' -%}
"{{ text }}"
"{{ text | strip }}"
// 2.输出
" Some potions create whitespace. "
"Some potions create whitespace."
newline_to_br
将字符串中的换行符 ( \n) 转换为 HTML 换行符 (
)
// 1.数据
{
"product": {
"description": "<h3>Are you low on health? Well we've got the potion just for you!</h3>\n<p>Just need a top up? Almost dead? In between? No need to worry because we have a range of sizes and strengths!</p>"
}
}
// 2.代码
{{ product.description | newline_to_br }}
// 3.输出
<h3>Are you low on health? Well we've got the potion just for you!</h3><br />
<p>Just need a top up? Almost dead? In between? No need to worry because we have a range of sizes and strengths!</p>
prepend
将指定字符串A添加到字符串B的开头。
// 1.数据
{
"product": {
"url": "/products/health-potion"
},
"request": {
"origin": "https://polinas-potent-potions.myshopify.com"
}
}
// 2.代码
{%- assign origin = request.origin -%}
{{ product.url | prepend: origin }}
// 3.输出
https://polinas-potent-potions.myfecify.com/products/health-potion
remove
删除字符串内的任何子字符串实例。
// 1.代码
{{ "I can't do it!" | remove: "'t" }}
// 2.输出
I can do it!
remove_first
删除字符串中子字符串的第一个实例。
// 1.代码
{{ "I hate it when I accidentally spill my duplication potion accidentally!" | remove_first: ' accidentally' }}
// 2.输出
I hate it when I spill my duplication potion accidentally!
replace
将字符串A,包含指的子字符串B,全部进行替换成字符串C
// 1.数据
{
"product": {
"handle": "komodo-dragon-scale"
}
}
// 2.代码
{{ product.handle | replace: '-', ' ' }}
// 3.输出
komodo dragon scale
replace_first
用给定的字符串,替换字符串中子字符串的第一个实例。
// 1.数据
{
"product": {
"handle": "komodo-dragon-scale"
}
}
// 2.代码
{{ product.handle | replace_first: '-', ' ' }}
// 3.输出
komodo dragon-scale
slice
返回从给定的基于 0 的索引开始的子字符串或一系列数组项。
默认情况下,子字符串的长度为一个字符,数组序列包含一个数组项。不过,您可以提供第二个参数来指定字符数或数组项的数量。
// 1.数据
{
"collection": {
"all_tags": [
"Burning",
"dried",
"extra-potent",
"extracts",
"fresh",
"healing",
"ingredients",
"music",
"plant",
"Salty",
"supplies"
],
"title": "Products"
}
}
// 2.代码
{{ collection.title | slice: 0 }}
{{ collection.title | slice: 0, 5 }}
{{ collection.all_tags | slice: 1, 2 | join: ', ' }}
// 3.输出
P
Produ
dried, extra-potent
负参数
您可以提供一个从字符串末尾开始计数的负索引。
// 1.数据
{
"collection": {
"title": "Products"
}
}
// 2.代码
{{ collection.title | slice: -5, 3 }}
// 3.输出(倒数第5个位置开始,然后向右取3个字符串)
duc
split
根据给定的分隔符将字符串拆分为子字符串数组。
// 1.数据
{
"product": {
"handle": "health-potion"
}
}
// 2.代码
{%- assign title_words = product.handle | split: '-' -%}
{% for word in title_words -%}
{{ word }}
{%- endfor %}
// 3.输出
health
potion
strip_html
从字符串中去除所有 HTML 标签。
// 1.数据
{
"product": {
"description": "<h3>Are you low on health? Well we've got the potion just for you!</h3>\n<p>Just need a top up? Almost dead? In between? No need to worry because we have a range of sizes and strengths!</p>"
}
}
// 2.代码
<!-- With HTML -->
{{ product.description }}
<!-- HTML stripped -->
{{ product.description | strip_html }}
// 3.输出
<!-- With HTML -->
<h3>Are you low on health? Well we've got the potion just for you!</h3>
<p>Just need a top up? Almost dead? In between? No need to worry because we have a range of sizes and strengths!</p>
<!-- HTML stripped -->
Are you low on health? Well we've got the potion just for you!
Just need a top up? Almost dead? In between? No need to worry because we have a range of sizes and strengths!
strip_newlines
从字符串中去除所有换行符(换行符)。
// 1.数据
{
"product": {
"description": "<h3>Are you low on health? Well we've got the potion just for you!</h3>\n<p>Just need a top up? Almost dead? In between? No need to worry because we have a range of sizes and strengths!</p>"
}
}
// 2.代码
<!-- With newlines -->
{{ product.description }}
<!-- Newlines stripped -->
{{ product.description | strip_newlines }}
// 3.输出
<!-- With newlines -->
<h3>Are you low on health? Well we've got the potion just for you!</h3>
<p>Just need a top up? Almost dead? In between? No need to worry because we have a range of sizes and strengths!</p>
<!-- Newlines stripped -->
<h3>Are you low on health? Well we've got the potion just for you!</h3><p>Just need a top up? Almost dead? In between? No need to worry because we have a range of sizes and strengths!</p>
truncate
将字符串截断为给定数量的字符。
如果指定的字符数小于字符串的长度,则会在截断的字符串后附加一个省略号 ( ... )。省略号包含在截断字符串的字符数中。
// 1.数据
{
"article": {
"title": "How to tell if you're out of invisibility potion"
}
}
// 2.代码
{{ article.title | truncate: 15, '...' }}
// 3.输出
How to tell if ...
truncatewords
1.将字符串截断为给定数量的单词。
如果指定的单词数少于字符串中的单词数,则会在截断的字符串后附加省略号 ( ... )。
- HTML 标签被视为单词,因此您应该从截断的内容中删除所有 HTML。如果您不删除 HTML,则可能会删除结束 HTML 标签,从而导致意外行为。
// 1.数据
{
"article": {
"content": "<p>We've all had this problem before: we peek into the potions vault to determine which potions we are running low on, and the invisibility potion bottle looks completely empty.</p>\n<p>...</p>\n<p> </p>"
}
}
// 2.代码
{{ article.content | strip_html | truncatewords: 15 }}
// 3.输出
We've all had this problem before: we peek into the potions vault to determine which...
2.指定自定义省略号
- 您可以提供第二个参数来指定自定义省略号。如果您不想要省略号,则可以提供一个空字符串。
// 1.数据
{
"article": {
"content": "<p>We've all had this problem before: we peek into the potions vault to determine which potions we are running low on, and the invisibility potion bottle looks completely empty.</p>\n<p>...</p>\n<p> </p>"
}
}
// 2.代码
{{ article.content | strip_html | truncatewords: 15, '--' }}
{{ article.content | strip_html | truncatewords: 15, '' }}
// 3.输出
We've all had this problem before: we peek into the potions vault to determine which--
We've all had this problem before: we peek into the potions vault to determine which
upcase
将字符串转换为全部大写字符。
// 1.数据
{
"product": {
"title": "Health potion"
}
}
// 2.代码
{{ product.title | upcase }}
// 3.输出
HEALTH POTION