slice can be used with arrays

This commit is contained in:
EricFromCanada
2021-02-22 16:39:10 -05:00
parent 9e0dc135ca
commit 09e4fef712

View File

@@ -1,11 +1,11 @@
---
title: slice
description: Liquid filter that returns a substring from a given position in a string.
description: Liquid filter that returns a substring or item from a given position in a string or array.
---
Returns a substring of 1 character beginning at the index specified by the first argument. An optional second argument specifies the length of the substring to be returned.
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 indices are numbered starting from 0.
String or array indices are numbered starting from 0.
<p class="code-label">Input</p>
```liquid
@@ -43,7 +43,23 @@ String indices are numbered starting from 0.
{{ "Liquid" | slice: 2, 5 }}
```
If the first argument is a negative number, the indices are counted from the end of the string:
Here the input value is an array:
<p class="code-label">Input</p>
```liquid
{% raw %}
{% assign beatles = "John, Paul, George, Ringo" | split: ", " %}
{{ beatles | slice: 1, 2 }}
{% endraw %}
```
<p class="code-label">Output</p>
```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.
<p class="code-label">Input</p>
```liquid