skip to Main Content

I have data set which holds, for instance, this syntax format: brand_shoetype_color (nike_270_red). I want to know how to (1) replace the underscore (_) with a gap (" ") and (2) capitalize each word in the new array.

Desire answers: Nike 270 Red.

2

Answers


  1. {%- assign your_str = "nike_270_red" -%}
    {%- assign words = your_str | split: "_" -%}
    {%- capture new_str -%}
      {%- for word in words %}{{ word | capitalize }} {% endfor -%}
    {%- endcapture -%}
    {{ new_str }}
    
    Login or Signup to reply.
  2. Here is a one liner just for fun:

    {{ 'nike_270_red' | replace: '_', '_zzzz_' | camelcase | replace: 'Zzzz', ' ' }}

    Where this _zzzz_ is just a placeholder that I remove afterwards with Zzzz.

    For reference I will probably not use it in a project but hey the more options the better.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search