Liam
Liam

Reputation: 20950

How do I remove up to a specified character from a string in Liquid?

I want to take a string such as "Alpha - Bravo - Charlie", and remove characters up to and including the first "-", leaving the result "Bravo - Charlie".

I can't find a function that will return the position of a character. I also can't find a function that will remove the first item from an array.

Upvotes: 0

Views: 1822

Answers (1)

Vladimir
Vladimir

Reputation: 2559

You can use the following solution:

{%- assign str = "Alpha - Bravo - Charlie" -%}
{%- assign str_parts = str | split: "-" -%}
{%- assign str_parts_size = str_parts | size -%}
{{- str_parts | slice: 1, str_parts_size | join: "-" -}}

Also, you can make it simpler by assuming a number of hyphens will never exceed a specific value e.g. 9:

{{- "Alpha - Bravo - Charlie" | split: "-" | slice: 1, 9 | join: "-" -}}

Upvotes: 1

Related Questions