slice Filter

Slices a string by an offset and length.

Parameters

Parameter Required Description
offset Yes integer Starting index within the string to slice. (The first character is at position 0.) Pass a negative value to start at the end of the string. (The last character is at position -1.)
length No integer Length of string to slice. If no value is provided, this defaults to 1.

Category

String Filters

Description

The slice filter allows you to slice a substring out of a string by an offset index and length.

Example: slicing from the beginning of a string
      {{ 'jello' | slice: 0 }}
      {{ 'jello' | slice: 1 }}
      {{ 'jello' | slice: 1, 3 }}
      

Output

      j
      e
      ell
      

When passing a negative value for the offset index, the slice filter will start at the last character and count backwards.

Example: slicing from the end of a string
      {{ 'jello' | slice: -1 }}
      {{ 'jello' | slice: -2, 2 }}
      {{ 'jello' | slice: -4, 3 }}
      

Output

      o
      lo
      ell