String filter are the most used filter in Liquid and usually transforms one string into another string. The following filter are supported:
This filter appends the parameter string to the input string.
Example:
{{ 'Hello' | append: ' world!' }}
{% assign title = product.title | append: ' (' | append: product.title.size | append: ')' %}
{{ title }}
Output:
Hello world!
Big Buck Bunny (14)
This filter will convert the text into camel-look-alike (each start character is upper case and connected directly to the last word).
Example:
{{ 'Hello world, here I come!' | camelize }}
Output:
HelloWorldHereICome
Converts the text into a string with the first character of each word made uppercase.
Example:
{{ 'Hello world, here I come!' | capitalize }}
Output:
Hello World, Here I Come!
Same as append but accept none string variables like integer
Example:
{{ subscription.runtime | concat: "Month" }}
Output:
10 Month
This filter will convert all letters to lowercase.
Example:
{{ 'HellO wORld, Here I cOMe!' | downcase }}
Output:
hello world, here i come!
Returns the first character of the input string.
Example:
{{ 'Hello world, here I come!' | first }}
Output:
H
The format filter allows you to format selected parts of a string. To control such values, add placeholders %0...%9 in the text
Example:
{{ "The subscription runs minimum %0 month and cost %1 per month" | format: subscription.initial_runtime,subscription.price_initial }}
Output:
The subscription runs minimum 10 month and cost 20.00$ per month
Returns the last character of the input string.
Example:
{{ 'Hello world, here I come!' | last }}
Output:
!
This filter removes all whitespaces on the left side of a string (in comparison to the strip and rstrip filter).
Example:
-{{ ' A lot of whitespace left and right ' | lstrip }}-
Output:
-A lot of whitespace left and right
-
Depending on the int input value, either the first (input equals 1) or second parameter (input doesn’t equal 1) is chosen.
Example:
{{ 1 | pluralize: 'Item', 'Items' }}
{{ 2 | pluralize: 'Item', 'Items' }}
Output:
Item
Items
Similar to append, this filter prepends the parameter to the input string.
Example:
{{ ' world!' | prepend: 'Hello' }}
Output:
Hello world!
The replace filter replaces all occurrences of the first parameter string within the input string by the second parameter (or just removes it if there is no second parameter).
Example:
{{ 'Welcome home - only home is home' | replace: 'home' }}
{{ 'Welcome home - only home is home' | replace: 'home', 'to the Pleasuredome' }}
Output:
Welcome - only is
Welcome to the Pleasuredome - only to the Pleasuredome is to the Pleasuredome
Similar to the replace filter, but only replaces the first occurrence of the first parameter string within the input string (either by the second parameter, or removes the occurrence if there is no second parameter).
Example:
{{ 'Welcome home - only home is home' | replace_first: 'home' }}
{{ 'Welcome home - only home is home' | replace_first: 'home', 'to the Pleasuredome' }}
Output:
Welcome - only home is home
Welcome to the Pleasuredome - only home is home
The remove filter removes all occurrences of the parameter string within the input string.
Example:
{{ 'Welcome home - only home is home' | remove: 'home' }}
Output:
Welcome - only is
Similar to the remove filter, but only removes the first occurrence of the parameter string within the input string.
Example:
{{ 'Welcome home - only home is home' | remove_first: 'home' }}
Output:
Welcome - only home is home
This filter removes all whitespaces on the right side of a string (in comparison to the strip and lstrip filter).
Example:
-{{ ' A lot of whitespace left and right ' | rstrip }}-
Output:
- A lot of whitespace left and right-
Returns the length of the input string (including all whitespaces).
Example:
{{ 'How about' | size }}
Output:
9
Returns the substring starting from a given position. If a second parameter is specified the substring will be of that length. If the position is negative, the position is counted from the end of the string.
Example:
{{ 'hello' | slice: 3 }}
{{ 'hello' | slice: -4, 2 }}
Output:
lo
el
This filter removes all whitespaces on both sides of a string (in comparison to the lstrip and rstrip filter).
Example:
-{{ ' A lot of whitespace left and right ' | strip }}-
Output:
-A lot of whitespace left and right-
Removes any HTML tags from the input string.
Example:
{{ '<a href="/"><i>Welcome home</i></a>' | strip_html }}
Output:
Welcome home
This filter repeats the input string a given number of times.
Example:
{{ 'Hello ' | times: 5 }}
Output:
Hello Hello Hello Hello Hello
Truncates an input string to the given length and add a truncate string (if truncated). The truncate string counts to the target length. If no length is specified, it will be truncated to 50 characters. If no truncate string is given, the default is …
Example:
{{ 'Hello world, this is my first text which I want to share with you!' | truncate }}
{{ 'Hello world, this is my first text which I want to share with you!' | truncate: 8 }}
{{ 'Hello world, this is my first text which I want to share with you!' | truncate: 10, '>' }}
Output:
Hello world, this is my first text which I want...
Hello...
Hello wor>
This filter operates similarly to the truncate filter, but truncates after a given number of words instead of characters. The parameters are the number of words after which to truncate and the truncation string (which defaults to …). If no number of words is specified, it will truncate after 15 words.
Example:
{{ 'Hello world, this is my first text which I want to share with you and your friends!' | truncate_words }}
{{ 'Hello world, this is my first text which I want to share with you and your friends!' | truncate_words: 8 }}
{{ 'Hello world, this is my first text which I want to share with you and your friends!' | truncate_words: 10, '>' }}
Output:
Hello world, this is my first text which I want to share with you and...
Hello world, this is my first text which...
Hello world, this is my first text which I want>
Converts the input string in uppercase.
Example:
{{ 'Hello world!' | upcase }}
Output:
HELLO WORLD!