Split string with liquid removes empty values at end?

Highlighted
Shopify Partner
2 0 1

Hi,

I found what I would consider a bug but maybe it's just undocumented expected behavior?

If you drop {{ "abcccc" | split:"c" | json }} into a liquid file the out put will be ["ab"]. I would have  expected ["ab","","","",""].

And it's not the json filter... when checking the size or doing a for loop there is only one value.

Any thoughts?

1 Like
Shopify Expert
24 0 4

I have the same problem 3 years later... No response?   

It is very easy to duplicate.   Trailing blank strings are dropped after split.

0 Likes
Highlighted
Shopify Partner
1373 186 446

I would have expected ["ab"] myself, but I've never split a string by a character that had multiple occurrences like that. I see in Javascript it is works in the way OP expected. Is there any reason you would want empty string values? Just curious as to what the use case is.

If my solution helped you, please like and accept as solution!
If you'd like to make any edits to your store, please send me a personal message and we can discuss what you'd like to accomplish :D
0 Likes
Highlighted
Shopify Expert
24 0 4

If I have data stored and I want to assign it to an array and loop, I'd like the arrays to come out the proper length.  It makes no sense to chop the last value.  Especially if I decide to pipe to json and assign to a javascript variable.

CODE

{%- assign delim = "^^" -%}
{%- assign titles = "title1^^" -%}
{%- assign subtitles = "^^subtitle1" -%}
{%- assign title_array = titles | split: delim -%}
{%- assign subtitle_array = subtitles | split: delim -%}
title_array: {{title_array | json}}
subtitle_array: {{subtitle_array | json}}

 

RESULT

title_array: ["title1"]

subtitle_array: ["","subtitle1"]

 

title_array should be ["title1", ""]

 

 

0 Likes