<p>The other way is you can use array map for this.</p>
<pre><code>{% set array_checked_values = submission|split(',')|map(c =&gt; c|trim) %}
</code></pre>

CC BY-SA 4.0
2022-09-07T18:39:13.770Z
Thank you very much, it works well and simpler than my code.
Kota.M
CC BY-SA 4.0
2022-09-07T04:20:45.537Z
2022-09-07T04:20:45.537Z
aodihis
<p>I have resolved it by myself. Although it will be an roundabout way, create another array. When each of the values is migrated, the string can be trimmed.</p>
<pre><code>{% set array_checked_values = submission[field.handle]|split(',') %}
{% set array_checked_values_2 = [] %}

{% for val in array_checked_values %}
    {% set array_checked_values_2 = array_checked_values_2|merge([val|trim]) %}
{% endfor %}
</code></pre>
<p>Then, both A and B above return true.</p>

CC BY-SA 4.0
2022-09-06T19:01:26.167Z
2022-09-06T19:01:26.167Z
Kota.M
<p>I am looking for how to trim each string in an array, in Craft 3.</p>
<p>I would like to check if a string is in an array of strings. The array is from a form submission, a checkbox group.</p>
<p>For example, there are two checkboxes and both of them are checked.</p>
<pre><code>{% set array_checked_values = submission|split(',') %}

A: {{ array_checked_values[0] == &quot;Value A&quot; }}
B: {{ array_checked_values[1] == &quot;Value B&quot; }}
</code></pre>
<p>Although I do not know why, <strong>only A returns true, but B is false.</strong></p>
<p>However, it returns true.</p>
<pre><code>B': {{ array_checked_values[1]|trim == &quot;Value B&quot; }}
</code></pre>
<p>So, I need to trim each string in the array, but I do not know how to do that. None of these three method work.</p>
<pre><code>{% set array_checked_values = submission|split(',')|trim %}

{% set array_checked_values = array_checked_values|trim %}

{% for val in array_checked_values %}
    {% set val = val|trim %}
{% endfor %}
</code></pre>

<craft3><templating><array>
How to trim each string in an array?
# How to trim each string in an array?

(Asked by: username_0 on 2022-09-06)

I am looking for how to trim each string in an array, in Craft 3.
I would like to check if a string is in an array of strings. The array is from a form submission, a checkbox group.
For example, there are two checkboxes and both of them are checked.
{% set array_checked_values = submission|split(',') %}

A: {{ array_checked_values[0] == "Value A" }}
B: {{ array_checked_values[1] == "Value B" }}

Although I do not know why, only A returns true, but B is false.
However, it returns true.
B': {{ array_checked_values[1]|trim == "Value B" }}

So, I need to trim each string in the array, but I do not know how to do that. None of these three method work.
{% set array_checked_values = submission|split(',')|trim %}

{% set array_checked_values = array_checked_values|trim %}

{% for val in array_checked_values %}
    {% set val = val|trim %}
{% endfor %}

**Tags:** craft3, templating, array

---

## 2 answers

### Answer by username_1 on 2022-09-07. Score: 1

The other way is you can use array map for this.
{% set array_checked_values = submission|split(',')|map(c => c|trim) %}

> **Comments:**
> **username_0**: Thank you very much, it works well and simpler than my code.  

---

### Answer by username_0 on 2022-09-06. Score: 0

I have resolved it by myself. Although it will be an roundabout way, create another array. When each of the values is migrated, the string can be trimmed.
{% set array_checked_values = submission[field.handle]|split(',') %}
{% set array_checked_values_2 = [] %}

{% for val in array_checked_values %}
    {% set array_checked_values_2 = array_checked_values_2|merge([val|trim]) %}
{% endfor %}

Then, both A and B above return true.


---

