749 votes
1 answers
shopify - 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.
Undefined asked
38
votes
Answer
Solution:
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: "-" -}}
Undefined answered
Source
Didn't find the answer?
Our community is visited by hundreds of Shopify development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.