# String filters

String filters change outputs and variables of type string.

String filters change outputs and variables of type [string](https://developer.versacommerce.de/articles/liquid-basics-types#string).

## append

Appends characters to a string.

```liquid
{{ 'sales' | append: '.jpg' }}
```

```text
sales.jpg
```

## base64_encode

Encodes a string to Base64.

```liquid
{{ 'one two three' | base64_encode }}
```

```text
b25lIHR3byB0aHJlZQ==
```

## base64_decode

Decodes a string from Base64.

```liquid
{{ 'b25lIHR3byB0aHJlZQ==' | base64_decode }}
```

```text
one two three
```

## base64_url_safe_encode

Encodes a string to URL-safe Base64. To create URL-safe Base64, the filter uses `-` and `_` instead of `+` and `/`.

```liquid
{{ '<p>ok?</p>' | base64_url_safe_encode }}
```

```text
PHA-b2s_PC9wPg==
```

## base64_url_safe_decode

Decodes a string from URL-safe Base64.

```liquid
{{ 'PHA-b2s_PC9wPg==' | base64_url_safe_decode }}
```

```text
<p>ok?</p>
```

## camelcase

Converts a string to CamelCase.

```liquid
{{ 'coming-soon' | camelcase }}
```

```text
ComingSoon
```

## capitalize

Capitalizes the first word in a string.

```liquid
{{ 'capitalize me' | capitalize }}
```

```text
Capitalize me
```

## downcase

Converts a string to lowercase.

```liquid
{{ 'UPPERCASE' | downcase }}
```

```text
uppercase
```

## escape

Escapes a string.

```liquid
{{ "<p>test</p>" | escape }}
```

```html
&lt;p&gt;test&lt;/p&gt;
<!-- Note: a browser shows this as <p>test</p> -->
```

## handleize

Formats a string as a [handle](https://developer.versacommerce.de/articles/liquid-basics-handle). In VersaCommerce the filter is called `handleize`; there is no short name `handle`.

The string is converted to lowercase and all spaces and special characters are replaced by hyphens (-). Several consecutive spaces or special characters are replaced by a single hyphen. Spaces and special characters at the start and end are removed.

```liquid
{{ '100% M & Ms!!!' | handleize }}
```

```text
100-m-ms
```

## md5

Converts a string to an MD5 hash.

One use of this filter is showing the Gravatar image of the author of a blog comment:

```liquid
<img src="https://www.gravatar.com/avatar/{{ comment.email | remove: ' ' | strip_newlines | downcase | md5 }}">
```

```html
<img src="https://www.gravatar.com/avatar/2a95ab7c950db9693c2ceb767784c201">
```

## sha1

Converts a string to a SHA-1 hash.

```liquid
{% assign my_secret_string = 'VersaCommerceIsAwesome!' | sha1 %}
My encoded string is: {{ my_secret_string }}
```

```text
My encoded string is: c2dbeca80d33cce5249d4b1448a470e2bee95860
```

## sha256

Converts a string to a SHA-256 hash.

```liquid
{% assign my_secret_string = 'VersaCommerceIsAwesome!' | sha256 %}
My encoded string is: {{ my_secret_string }}
```

```text
My encoded string is: db3acf2a3c5295667e95e3761a548abcf22452333517508162aae0b75c39656a
```

## hmac_sha1

Converts a string to a SHA-1 hash with a hash-based message authentication code (HMAC). Pass the secret key for the message as a parameter to the filter.

```liquid
{% assign my_secret_string = 'VersaCommerceIsAwesome!' | hmac_sha1: 'secret_key' %}
My encoded string is: {{ my_secret_string }}
```

```text
My encoded string is: eeb5478803ef805b3d4674fb021d600b84a4753c
```

## hmac_sha256

Converts a string to a SHA-256 hash with a hash-based message authentication code (HMAC). Pass the secret key for the message as a parameter to the filter.

```liquid
{% assign my_secret_string = 'VersaCommerceIsAwesome!' | hmac_sha256: 'secret_key' %}
My encoded string is: {{ my_secret_string }}
```

```text
My encoded string is: 80f24c762dcf04c7d3ce150c4379cb4dc88688f3fb0ae2598b324e88f56ec47b
```

## newline_to_br

Inserts a `<br>` line break HTML tag before each line break in a string.

```liquid
{% capture var %}
One
Two
Three
{% endcapture %}
{{ var | newline_to_br }}
```

```html
One<br>
Two<br>
Three<br>
```

## pluralize

Outputs the singular or plural form of an English string based on a number. The first parameter is the singular, the second the plural.

`pluralize` applies English plural rules. Do not use it for strings in other languages, because it produces wrong plurals there.

```liquid
{{ cart.item_count }}
{{ cart.item_count | pluralize: 'item', 'items' }}
```

```text
3 items
```

## prepend

Prepends characters to a string.

```liquid
{{ 'sale' | prepend: 'Made a great ' }}
```

```text
Made a great sale
```

## remove

Removes all occurrences of a substring from a string.

```liquid
{{ 'Hello, world. Goodbye, world.' | remove: 'world' }}
```

```text
Hello, . Goodbye, .
```

## remove_first

Removes only the first occurrence of a substring from a string.

```liquid
{{ 'Hello, world. Goodbye, world.' | remove_first: 'world' }}
```

```text
Hello, . Goodbye, world.
```

## replace

Replaces all occurrences of a string with a substring.

```liquid
<!-- product.title = "Awesome Shoes" -->
{{ product.title | replace: 'Awesome', 'Mega' }}
```

```text
Mega Shoes
```

## replace_first

Replaces the first occurrence of a string with a substring.

```liquid
<!-- product.title = "Awesome Awesome Shoes" -->
{{ product.title | replace_first: 'Awesome', 'Mega' }}
```

```text
Mega Awesome Shoes
```

## slice

`slice` returns a substring that starts at the given index. An optional second parameter sets the length of the substring. Without a second parameter, a substring of one character is returned.

```liquid
{{ 'hello' | slice: 0 }}
{{ 'hello' | slice: 1 }}
{{ 'hello' | slice: 1, 3 }}
```

```text
h
e
ell
```

If the index is negative, it is counted from the end of the string.

```liquid
{{ 'hello' | slice: -3, 2  }}
```

```text
ll
```

## split

`split` takes a substring as parameter. The substring is used as delimiter to split a string into an array. Use [array filters](https://developer.versacommerce.de/articles/liquid-filters-array-filters) to output parts of the array.

```liquid
{% assign words = 'Hi, how are you today?' | split: ' ' %}

{% for word in words %}
{{ word }}
{% endfor %}
```

```text
Hi,
how
are
you
today?
```

## strip

Removes tabs, spaces and line breaks (all whitespace) from the left and right side of a string.

```liquid
{{ '   too many spaces      ' | strip }}
```

```text
too many spaces
```

## lstrip

Removes tabs, spaces and line breaks (all whitespace) from the **left** side of a string.

```liquid
{{ '   too many spaces           ' | lstrip }}
```

```text
too many spaces           
```

## rstrip

Removes tabs, spaces and line breaks (all whitespace) from the **right** side of a string.

```liquid
{{ '              too many spaces      ' | rstrip }}
```

```text
              too many spaces
```

## strip_html

Removes all HTML tags from a string.

```liquid
{{ '<h1>Hello</h1> World' | strip_html }}
```

```text
Hello World
```

## strip_newlines

Removes all line breaks from a string.

```liquid
{{ product.description | strip_newlines }}
```

## truncate

Truncates a string to the number of characters passed as first parameter. An ellipsis (...) is appended to the truncated string and counts towards the number of characters.

```liquid
{{ 'The cat came back the very next day' | truncate: 13 }}
```

```text
The cat ca...
```

## Custom ellipsis

`truncate` takes an optional second parameter with the string to append to the truncated string. By default this is an ellipsis (...), but you can pass another sequence.

The length of the second parameter counts towards the number of characters set by the first parameter. For example, to truncate a string to exactly 10 characters and use a 3-character ellipsis, pass **13** as first parameter, because the ellipsis counts as 3 characters.

```liquid
{{ 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' | truncate: 18, ', and so on' }}
```

```text
ABCDEFG, and so on
```

## No ellipsis

Pass an empty string as second parameter to truncate to the exact number of characters given in the first parameter without trailing characters:

```liquid
{{ "I'm a little teapot, short and stout." | truncate: 15, '' }}
```

```text
I'm a little te
```

## truncatewords

Truncates a string to the number of words passed as first parameter. An ellipsis (...) is appended to the truncated string.

```liquid
{{ 'The cat came back the very next day' | truncatewords: 4 }}
```

```text
The cat came back...
```

## Custom ellipsis

`truncatewords` takes an optional second parameter with the string to append to the truncated string. By default this is an ellipsis (...), but you can pass another sequence.

```liquid
{{ 'The cat came back the very next day' | truncatewords: 4, '--' }}
```

```text
The cat came back--
```

## No ellipsis

Pass an empty string as second parameter to avoid trailing characters:

```liquid
{{ 'The cat came back the very next day' | truncatewords: 4, '' }}
```

```text
The cat came back
```

## upcase

Converts a string to uppercase.

```liquid
{{ 'i want this to be uppercase' | upcase }}
```

```text
I WANT THIS TO BE UPPERCASE
```

## url_encode

Converts all URL-unsafe characters in a string to percent-encoded characters.

```liquid
{{ 'john@liquid.com' | url_encode }}
```

```text
john%40liquid.com
```

`url_encode` converts a space into a `+` character instead of a percent-encoded character.

```liquid
{{ 'Jane Doe' | url_encode }}
```

```text
Jane+Doe
```

## url_escape

Finds all characters in a string that are not allowed in URLs and replaces them with their escaped variants.

```liquid
{{ '<hello> & <world>' | url_escape }}
```

```text
%3Chello%3E%20&%20%3Cworld%3E
```

## url_param_escape

Replaces all characters in a string that are not allowed in URLs with their escaped variants, including the ampersand (&).

```liquid
{{ '<hello> & <world>' | url_param_escape }}
```

```text
%3Chello%3E%20%26%20%3Cworld%3E
```

## Reversing strings

`reverse` cannot be applied to a string directly, but you can split a string into an array, reverse the array and join it again by chaining filters:

```liquid
{{ 'Ground control to Major Tom.' | split: '' | reverse | join: '' }}
```

```text
.moT rojaM ot lortnoc dnuorG
```
