Skip to main content

"Starts With" Filter for Liquid Markup

Liquid doesn't have a native "string starts with" filter, but you can easily build one with slice.

Let's say we want to see if input "reworkflow" starts with prefix "rew." We doย not want "brewing" to match. First, we slice off characters 0 through 3 (prefix length) of the input. Then we compare whether the sliced portion matches the prefix.

Example using merge fields {{stringToCheck}} and {{startsWith}}:

{% assign checkSlice = stringToCheck | slice: 0, startsWith.size %}
{% if checkSlice == startsWith %}
  The string {{stringToCheck}} does start with {{startsWith}}.
  {% else %}
  The string {{stringToCheck}} does NOT start with {{startsWith}}.
{% endif %}

Stand-alone example that uses variables instead of merge fields:

{% assign stringToCheck = 'reworkflow' %}
{% assign startsWith = 'rew' %}
{% assign checkSlice = stringToCheck | slice: 0, startsWith.size %}
{% if checkSlice == startsWith %}
  The string {{stringToCheck}} does start with {{startsWith}}.
  {% else %}
  The string {{stringToCheck}} does NOT start with {{startsWith}}.
{% endif %}