# 字符串过滤器

> Shoplazza Theme 的 Liquid 字符串过滤器：append、replace、truncate、handleize、escape、upcase 以及各类哈希等字符串处理。

字符串过滤器用于修改[字符串](/zh-CN/docs/theme/references/liquid/basics/liquid-types#string)。

## append

`string | append: string`

returns  [string](../basics/liquid-types#string)

将给定字符串添加到字符串的末尾。

**代码**

```liquid
{{ shop.url | append: product.url }}
```

**数据**

```json
{
  "product": {
    "url": "/products/reverse-short-sleeve-tee"
  }
}
```

**输出**

```html
https://shop.myshoplaza.com/products/reverse-short-sleeve-tee
```

## camelcase

`string | camelcase`

returns  [string](../basics/liquid-types#string)

将字符串转换为驼峰命名法（CamelCase）。

**代码**

```liquid
{{ 'camel-case' | camelcase }}
```

**输出**

```html
CamelCase
```

## capitalize

`string | capitalize`

returns  [string](../basics/liquid-types#string)

将字符串中每个单词的首字母大写。

**代码**

```liquid
{{ 'this sentence should start with a capitalized word.' | capitalize }}
```

**输出**

```html
This Sentence Should Start With A Capitalized Word.
```

## downcase

`string | downcase`

returns  [string](../basics/liquid-types#string)

将字符串转换为全小写字符。

**代码**

```liquid
{{ product.title | downcase }}
```

**数据**

```json
{
  "product": {
    "title": "Ace Cashmere Beanie"
  }
}
```

**输出**

```html
ace cashmere beanie
```

## escape

`string | escape`

returns  [string](../basics/liquid-types#string)

转义 HTML 中的特殊字符（例如 `<>`、`'`、`"` 和 `&`），并将这些字符转换为转义序列。该过滤器不会影响字符串中没有对应转义序列的字符。

**代码**

```liquid
{{ '<p>Text to be escaped.</p>' | escape }}
```

**输出**

```html
&lt;p&gt;Text to be escaped.&lt;/p&gt;
```

## escape_once

`string | escape_once`

returns  [string](../basics/liquid-types#string)

转义字符串，但不会改变已经转义过的字符。

**代码**

```liquid
{% assign escaped_text = '<p>Text to be escaped.</p>' | escape %}

{{ escaped_text }}
{{ escaped_text | escape_once }}
```

**输出**

```html
&lt;p&gt;Text to be escaped.&lt;/p&gt;
&lt;p&gt;Text to be escaped.&lt;/p&gt;
```

## handleize

`string | handleize`

returns  [string](../basics/liquid-types#string)

将字符串转换为 [handle](/zh-CN/docs/theme/references/liquid/basics/handles)。

**备注**

`handleize` 过滤器有一个别名 `handle`。

**代码**

```liquid
{{ product.title | handleize }}
{{ product.title | handle }}
```

**数据**

```json
{
  "product": {
    "title": "Ace Cashmere Beanie"
  }
}
```

**输出**

```html
ace-cashmere-beanie
ace-cashmere-beanie
```

## hmac_sha1

`string | hmac_sha1: string`

returns  [string](../basics/liquid-types#string)

使用哈希消息认证码（HMAC）将字符串转换为 SHA-1 哈希。

消息的密钥作为参数提供给过滤器。

**代码**

```liquid
{{ 'Ace' | hmac_sha1: 'Salty' }}
```

**输出**

```html
5da24ef75f6a8fc7d28c0fcc7236244d29a4511d
```

## hmac_sha256

`string | hmac_sha256: string`

returns  [string](../basics/liquid-types#string)

使用哈希消息认证码（HMAC）将字符串转换为 SHA-256 哈希。

消息的密钥作为参数提供给过滤器。

**代码**

```liquid
{{ 'Ace' | hmac_sha256: 'Salty' }}
```

**输出**

```html
8f688efb30c4e26bed3e14923030f9276e463e7933c88fc18c0ff0c934185418
```

## lstrip

`string | lstrip`

returns  [string](../basics/liquid-types#string)

删除字符串左侧的所有空白字符。

**代码**

```liquid
{% assign text = '  Some fruits create whitespace.    ' %}
"{{ text }}"
"{{ text | lstrip }}"
```

**输出**

```html
" Some fruits create whitespace. "
"Some fruits create whitespace. "
```

## md5

`string | md5`

returns  [string](../basics/liquid-types#string)

将字符串转换为 MD5 哈希。

**代码**

```liquid
{{ '' | md5 }}
```

**输出**

```html
d41d8cd98f00b204e9800998ecf8427e
```

## newline_to_br

`string | newline_to_br`

returns  [string](../basics/liquid-types#string)

将字符串中的换行符（`\n`）转换为 HTML 换行标签（`<br>`）。

**代码**

```liquid
{% capture description %}
  <h3>Sound inspired</h3>
  <div>Get inspired with Solo Pro wireless headphones.</div>
{% endcapture %}

{{ description | newline_to_br }}
```

**输出**

```html
<br>
<h3>Sound inspired</h3>
<br>
<div>Get inspired with Solo Pro wireless headphones.</div>
<br>
```

## pluralize

`number | pluralize: string, string`

returns  [string](../basics/liquid-types#string)

根据给定数字输出字符串的单数或复数形式。

**代码**

```liquid
{% assign item_count = cart.cart %}

Cart item count: {{ item_count }} {{ item_count | pluralize: 'item', 'items' }}
```

**数据**

```json
{
  "cart": {
    "item_count": 2
  }
}
```

**输出**

```html
Cart item count: 2 items
```

## prepend

`string | prepend: string`

returns  [string](../basics/liquid-types#string)

将给定字符串添加到字符串的开头。

**代码**

```liquid
{{ product.url | prepend: shop.url }}
```

**数据**

```json
{
  "product": {
    "url": "/products/reverse-short-sleeve-tee"
  }
}
```

**输出**

```html
https://shop.myshoplaza.com/products/reverse-short-sleeve-tee
```

## remove

`string | remove: string`

returns  [string](../basics/liquid-types#string)

删除字符串中每个子字符串实例。

**代码**

```liquid
{{ "I can't do it!" | remove: "'t" }}
```

**输出**

```html
I can do it!
```

## remove_first

`string | remove_first: string`

returns  [string](../basics/liquid-types#string)

删除字符串中第一个子字符串实例。

**代码**

```liquid
{{ "I don't know why, but I don't know how to do it." | remove_first: "don't" }}
```

**输出**

```html
I know why, but I don't know how to do it.
```

## replace

`string | replace: string, string`

returns  [string](../basics/liquid-types#string)

将字符串中每个子字符串实例替换为给定字符串。

**代码**

```liquid
{{ product.handle | replace: '-', ' ' }}
```

**数据**

```json
{
  "product": {
    "handle": "ace-cashmere-beanie"
  }
}
```

**输出**

```html
ace cashmere beanie
```

## replace_first

`string | replace_first: string, string`

returns  [string](../basics/liquid-types#string)

将字符串中第一个子字符串实例替换为给定字符串。

**代码**

```liquid
{{ product.handle | replace_first: '-', ' ' }}
```

**数据**

```json
{
  "product": {
    "handle": "ace-cashmere-beanie"
  }
}
```

**输出**

```html
ace cashmere-beanie
```

## rstrip

`string | rstrip`

returns  [string](../basics/liquid-types#string)

删除字符串右侧的所有空白字符。

**代码**

```liquid
{% assign text = '  Some fruits create whitespace.    ' %}
"{{ text }}"
"{{ text | rstrip }}"
```

**输出**

```html
" Some fruits create whitespace. "
" Some fruits create whitespace."
```

## sha1

`string | sha1`

returns  [string](../basics/liquid-types#string)

将字符串转换为 SHA-1 哈希。

**代码**

```liquid
{{ 'Ace' | sha1 }}
```

**输出**

```html
fc2b5a9cb80ab0a1526f4fbc887646dd245632c2
```

## sha256

`string | sha256`

returns  [string](../basics/liquid-types#string)

将字符串转换为 SHA-256 哈希。

**代码**

```liquid
{{ 'Ace' | sha256 }}
```

**输出**

```html
a467da82da540f56097b7224d81612bfa212e26efbf0709606144fee0b82a631
```

## slice

`string | slice`

returns  [string](../basics/liquid-types#string)

从给定的从 0 开始的索引位置起，返回一个子字符串或一系列数组元素。

默认情况下，子字符串长度为一个字符，数组系列包含一个数组元素。不过，您可以提供第二个参数来指定字符数或数组元素的数量。

**代码**

```liquid
{{ collection.title | slice: 0 }}
{{ collection.title | slice: 0, 3 }}
{{ collection.tags | slice: 1, 2 | join: ', ' }}
```

**数据**

```json
{
  "collection": {
    "tags": [
      "Burning",
      "fresh",
      "music",
      "plant",
      "Salty"
    ],
    "title": "All products"
  }
}
```

**输出**

```html
All products
All
fresh, music
```

### 负索引

您可以提供一个负数索引，它将从字符串的末尾开始计数。

**代码**

```liquid
{{ title | slice: -8, 8 }}
```

**数据**

```json
{
  "collection": {
    "title": "All products"
  }
}
```

**输出**

```html
products
```

## split

`string | split: string`

returns  [string](../basics/liquid-types#string)

根据给定分隔符将字符串拆分为子字符串数组。

**代码**

```liquid
{% assign title_words = product.handle | split: '-' %}

{% for word in title_words %}
  {{ word }}
{% endfor %}
```

**数据**

```json
{
  "product": {
    "handle": "ace-cashmere-beanie"
  }
}
```

**输出**

```html
ace
cashmere
beanie
```

## strip

`string | strip`

returns  [string](../basics/liquid-types#string)

删除字符串左侧和右侧的所有空白字符。

**代码**

```liquid
{% assign text = '  Some fruits create whitespace.    ' %}
"{{ text }}"
"{{ text | strip }}"
```

**输出**

```html
" Some fruits create whitespace. "
"Some fruits create whitespace."
```

## strip_html

`string | strip_html`

returns  [string](../basics/liquid-types#string)

删除字符串中的所有 HTML tag。

**代码**

```liquid
<!-- With HTML -->
{{ product.description }}

<!-- HTML stripped -->
{{ product.description | strip_html }}
```

**数据**

```json
{
  "product": {
    "description": "<h3>Sound inspired</h3>\n<div>Get inspired with Solo Pro wireless headphones. To deliver sound how you want it, Solo Pro features two listening modes: Active Noise Cancelling (ANC) and Transparency mode.</div>"
  }
}
```

**输出**

```html
<!-- With HTML -->
<h3>Sound inspired</h3>
<div>Get inspired with Solo Pro wireless headphones. To deliver sound how you want it, Solo Pro features two listening modes: Active Noise Cancelling (ANC) and Transparency mode.</div>

<!-- HTML stripped -->
Sound inspired
Get inspired with Solo Pro wireless headphones. To deliver sound how you want it, Solo Pro features two listening modes: Active Noise Cancelling (ANC) and Transparency mode.
```

## strip_newlines

`string | strip_newlines`

returns  [string](../basics/liquid-types#string)

删除字符串中的所有换行符（行分隔符）。

**代码**

```liquid
<!-- With newlines -->
{{ product.description }}

<!-- Newlines stripped -->
{{ product.description | strip_newlines }}
```

**数据**

```json
{
  "product": {
    "description": "<h3>Sound inspired</h3>\n<div>Get inspired with Solo Pro wireless headphones. To deliver sound how you want it, Solo Pro features two listening modes: Active Noise Cancelling (ANC) and Transparency mode.</div>"
  }
}
```

**输出**

```html
<!-- With newlines -->
<h3>Sound inspired</h3>
<div>Get inspired with Solo Pro wireless headphones. To deliver sound how you want it, Solo Pro features two listening modes: Active Noise Cancelling (ANC) and Transparency mode.</div>

<!-- Newlines stripped -->
<h3>Sound inspired</h3><div>Get inspired with Solo Pro wireless headphones. To deliver sound how you want it, Solo Pro features two listening modes: Active Noise Cancelling (ANC) and Transparency mode.</div>
```

## truncate

`string | truncate: number`

returns  [string](../basics/liquid-types#string)

将字符串截断到给定的字符数。

如果指定的字符数小于字符串的长度，则会在截断后的字符串末尾追加一个省略号（`...`）。该省略号会计入截断后字符串的字符数。

**代码**

```liquid
{{ product.title | truncate: 10 }}
{{ product.title | truncate: 20 }}
```

**数据**

```json
{
  "product": {
    "title": "Ace Cashmere Beanie"
  }
}
```

**输出**

```html
Ace Cas...
Ace Cashmere Beanie
```

### 指定自定义省略号

`string | truncate: number, string`

您可以提供第二个参数来指定自定义省略号。如果您不想要省略号，则可以提供一个空字符串。

**代码**

```liquid
{{ product.title | truncate: 10, '' }}
{{ product.title | truncate: 10, '···' }}
```

**数据**

```json
{
  "product": {
    "title": "Ace Cashmere Beanie"
  }
}
```

**输出**

```html
Ace Cashme
Ace ···
```

## truncatebytes

`string | truncatebytes: number`

returns  [string](../basics/liquid-types#string)

将字符串截断到给定的字节数。

如果指定的字符数小于字符串的长度，则会在截断后的字符串末尾追加一个省略号（`...`）。该省略号会计入截断后字符串的字符数。

**代码**

```liquid
{{ product.description | truncatebytes: 50 }}
```

**数据**

```json
{
  "product": {
    "description": "Get inspired with Solo Pro wireless headphones. To deliver sound how you want it, Solo Pro features two listening modes: Active Noise Cancelling (ANC) and Transparency mode. Beats’ Pure ANC gives you the space to create with fully immersive sound, while Transparency mode helps you stay aware of your surroundings. Every detail of Solo Pro has been carefully considered, right down to the intuitive way the headphones turn on and off via folding. The ergonomic design delivers exceptional comfort for extended wear and sleek style. And with up to 22 hours of battery life, you can keep the music going no matter where your day takes you."
  }
}
```

**输出**

```html
Get inspired with Solo Pro wireless headphones....
```

### 指定自定义省略号

`string | truncatebytes: number, string`

您可以提供第二个参数来指定自定义省略号。如果您不想要省略号，则可以提供一个空字符串。

**代码**

```liquid
{{ product.description | truncatebytes: 50, '' }}
{{ product.description | truncatebytes: 50, '···' }}
```

**数据**

```json
{
  "product": {
    "description": "Get inspired with Solo Pro wireless headphones. To deliver sound how you want it, Solo Pro features two listening modes: Active Noise Cancelling (ANC) and Transparency mode. Beats’ Pure ANC gives you the space to create with fully immersive sound, while Transparency mode helps you stay aware of your surroundings. Every detail of Solo Pro has been carefully considered, right down to the intuitive way the headphones turn on and off via folding. The ergonomic design delivers exceptional comfort for extended wear and sleek style. And with up to 22 hours of battery life, you can keep the music going no matter where your day takes you."
  }
}
```

**输出**

```html
Get inspired with Solo Pro wireless headphones. To
Get inspired with Solo Pro wireless headphon···
```

## truncatewords

`string | truncatewords: number`

returns  [string](../basics/liquid-types#string)

将字符串截断到给定的单词数。

如果指定的单词数小于字符串中的单词数，则会在截断后的字符串末尾追加一个省略号（`...`）。

**代码**

```liquid
{{ product.description | truncatewords: 10 }}
```

**数据**

```json
{
  "product": {
    "description": "Get inspired with Solo Pro wireless headphones. To deliver sound how you want it, Solo Pro features two listening modes: Active Noise Cancelling (ANC) and Transparency mode. Beats’ Pure ANC gives you the space to create with fully immersive sound, while Transparency mode helps you stay aware of your surroundings. Every detail of Solo Pro has been carefully considered, right down to the intuitive way the headphones turn on and off via folding. The ergonomic design delivers exceptional comfort for extended wear and sleek style. And with up to 22 hours of battery life, you can keep the music going no matter where your day takes you."
  }
}
```

**输出**

```html
Get inspired with Solo Pro wireless headphones. To deliver sound...
```

### 指定自定义省略号

`string | truncatewords: number, string`

returns  [string](../basics/liquid-types#string)

您可以提供第二个参数来指定自定义省略号。如果您不想要省略号，则可以提供一个空字符串。

**代码**

```liquid
{{ product.description | truncatewords: 10, '' }}
{{ product.description | truncatewords: 10, '---' }}
```

**数据**

```json
{
  "product": {
    "description": "Get inspired with Solo Pro wireless headphones. To deliver sound how you want it, Solo Pro features two listening modes: Active Noise Cancelling (ANC) and Transparency mode. Beats’ Pure ANC gives you the space to create with fully immersive sound, while Transparency mode helps you stay aware of your surroundings. Every detail of Solo Pro has been carefully considered, right down to the intuitive way the headphones turn on and off via folding. The ergonomic design delivers exceptional comfort for extended wear and sleek style. And with up to 22 hours of battery life, you can keep the music going no matter where your day takes you."
  }
}
```

**输出**

```html
Get inspired with Solo Pro wireless headphones. To deliver sound
Get inspired with Solo Pro wireless headphones. To deliver sound---
```

## upcase

`string | upcase`

returns  [string](../basics/liquid-types#string)

将字符串转换为全大写字符。

**代码**

```liquid
{{ product.title | upcase }}
```

**数据**

```json
{
  "product": {
    "title": "Ace Cashmere Beanie"
  }
}
```

**输出**

```html
ACE CASHMERE BEANIE
```

## url_decode

`string | url_decode`

returns  [string](../basics/liquid-types#string)

解码字符串中所有[百分号编码](https://developer.mozilla.org/en-US/docs/Glossary/percent-encoding)的字符。

**代码**

```liquid
{{ 'https%3A%2F%2Fwww.shoplazza.com%2F' | url_decode }}
```

**输出**

```html
https://www.shoplazza.com/
```

## url_encode

`string | url_encode`

returns  [string](../basics/liquid-types#string)

将字符串中所有 URL 不安全的字符转换为对应的[百分号编码](https://developer.mozilla.org/en-US/docs/Glossary/percent-encoding)。

**备注**

空格会被转换为 `+` 字符，而非百分号编码的字符。

**代码**

```liquid
{{ 'https://www.shoplazza.com/' | url_encode }}
```

**输出**

```html
https%3A%2F%2Fwww.shoplazza.com%2F
```
