<p>Twig has a <a href="http://twig.sensiolabs.org/doc/filters/replace.html">replace filter</a> for that. You can replace strings/vars with strings/vars: </p>

<pre><code>{% set string = "I love apples" %}
{% set var = "like" %}
{% set string = string|replace({'apples': 'kiwis', 'love': var}) %}

{{ string }} {# Prints "I like kiwis" #}
</code></pre>

<p>Your example:</p>

<pre><code>{% set rgb = "Red; Green; Blue" %}
{% set rgb = rgb|replace({';': ','}) %}

{{ rgb }} {# Prints "Red, Green, Blue" #}
</code></pre>

CC BY-SA 4.0
2019-05-16T20:49:09.087Z
Thanks for this! Helped me out with replacing something with a variable.
mateostabio
CC BY-SA 3.0
2014-06-14T13:35:37.953Z
Worth noting that Craft actually comes with its own version of the replace filter, which actually supports a simpler syntax if you’re only replacing one thing: `|replace('apples', 'kiwis')`
Brandon Kelly
CC BY-SA 3.0
2014-06-14T11:19:30.177Z
2014-06-14T11:19:30.177Z
Victor
<p>How do I replace ";" with "," in string "Red; Green; Blue"?</p>

<pre><code>{% set rgb = "Red; Green; Blue" %}
</code></pre>

<string><replace>
How to replace characters in string
# How to replace characters in string

(Asked by: username_0 on 2014-06-14)

How do I replace ";" with "," in string "Red; Green; Blue"?
{% set rgb = "Red; Green; Blue" %}

**Tags:** string, replace

---

## 1 answers

### Answer by username_1 on 2014-06-14. Score: 21 | Accepted answer

Twig has a replace filter for that. You can replace strings/vars with strings/vars: 
{% set string = "I love apples" %}
{% set var = "like" %}
{% set string = string|replace({'apples': 'kiwis', 'love': var}) %}

{{ string }} {# Prints "I like kiwis" #}

Your example:
{% set rgb = "Red; Green; Blue" %}
{% set rgb = rgb|replace({';': ','}) %}

{{ rgb }} {# Prints "Red, Green, Blue" #}

> **Comments:**
> **username_3**: Worth noting that Craft actually comes with its own version of the replace filter, which actually supports a simpler syntax if you’re only replacing one thing: `|replace('apples', 'kiwis')`  
> **username_2**: Thanks for this! Helped me out with replacing something with a variable.  

---

