Files
liquid/_filters/slice.md
2021-04-28 13:43:02 -04:00

1.5 KiB

title, description
title description
slice Liquid filter that returns a substring or item from a given position in a string or array.

Returns a substring of one character or series of array items beginning at the index specified by the first argument. An optional second argument specifies the length of the substring or number of array items to be returned.

String or array indices are numbered starting from 0.

Input

```liquid {%- raw -%} {{ "Liquid" | slice: 0 }} {% endraw %} ```

Output

```text {{ "Liquid" | slice: 0 }} ```

Input

```liquid {%- raw -%} {{ "Liquid" | slice: 2 }} {% endraw %} ```

Output

```text {{ "Liquid" | slice: 2 }} ```

Input

```liquid {%- raw -%} {{ "Liquid" | slice: 2, 5 }} {% endraw %} ```

Output

```text {{ "Liquid" | slice: 2, 5 }} ```

Here the input value is an array:

Input

```liquid {%- raw -%} {% assign beatles = "John, Paul, George, Ringo" | split: ", " %} {{ beatles | slice: 1, 2 }} {% endraw %} ```

Output

```text {% assign beatles = "John, Paul, George, Ringo" | split: ", " %} {{ beatles | slice: 1, 2 }} ```

If the first argument is a negative number, the indices are counted from the end of the string.

Input

```liquid {%- raw -%} {{ "Liquid" | slice: -3, 2 }} {% endraw %} ```

Output

```text {{ "Liquid" | slice: -3, 2 }} ```