# How do I concatenate strings with HUBL?

**URL:** <https://community.hubspot.com/t/how-do-i-concatenate-strings-with-hubl/12876>\
**Category:** CMS Development\
**Created:** [February 8, 2019, 4:38pm UTC](https://community.hubspot.com/t/how-do-i-concatenate-strings-with-hubl/12876 "2019-02-08T16:38:04Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![BillOddie](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/billoddie/32/31184_2.png) [@BillOddie](https://community.hubspot.com/u/BillOddie)\
**Post date:** [February 8, 2019, 4:38pm UTC](https://community.hubspot.com/t/how-do-i-concatenate-strings-with-hubl/12876/1 "2019-02-08T16:38:04Z")

</div>

How can I concatenate strings with Hubl? The docs suggest there is a special operator for it ([~](https://designers.hubspot.com/docs/hubl/operators-and-expression-tests#other-hubl-operators)), however I have tried to use it in many different ways and I can’t preduce the behaviour I expect.

I need to loop through an list and build up a string with the contents of the list. I have tried this and after the loop the ‘items’ variable has no value…

{% set items = “” %}

{% for row in hubdb\_table\_rows(1034058, “orderBy=random()”) %}  
{{ items = items ~ (“Test”+loop.index0+", ") }}  
{% endfor %}  
\<div\>{{ items }}\</div\>

Has anyone managed to do this before?

---

<div class="post-metadata">

**Author:** ![Jon\_McLaren](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/jon_mclaren/32/8271_2.png) [@Jon\_McLaren](https://community.hubspot.com/u/Jon_McLaren)\
**Post date:** [February 8, 2019, 6:34pm UTC](https://community.hubspot.com/t/how-do-i-concatenate-strings-with-hubl/12876/2 "2019-02-08T18:34:19Z")

</div>

```auto
{% set items = [] %}
{% for x in array %}
 {% set _dummyvar = items.append(x) %}
{%endfor %}

{{items}}

```

This wont get you 100% of the way but will get you most of the way. Wasn’t entirely sure I understood what you were intending to do.

---

<div class="post-metadata">

**Author:** ![stefen](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/stefen/32/29274_2.png) [@stefen](https://community.hubspot.com/u/stefen)\
**Post date:** [February 8, 2019, 7:52pm UTC](https://community.hubspot.com/t/how-do-i-concatenate-strings-with-hubl/12876/3 "2019-02-08T19:52:03Z")

</div>

@BillOddie you’ll want to use the tilde in the statement rather than the expression. For example:

`{% set items = "string 1" ~ "string 2" %}`

Hope that helps!

---

<div class="post-metadata">

**Author:** ![BillOddie](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/billoddie/32/31184_2.png) [@BillOddie](https://community.hubspot.com/u/BillOddie)\
**Post date:** [February 11, 2019, 10:01am UTC](https://community.hubspot.com/t/how-do-i-concatenate-strings-with-hubl/12876/4 "2019-02-11T10:01:28Z")

</div>

Hello,

Thank you both for your answers.

I ended up using join| filter to print the list as a string with a custom delimiter.

Sorry if my question was confusing

---

<div class="post-metadata">

**Author:** ![piersg](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/piersg/32/99782_2.png) [@piersg](https://community.hubspot.com/u/piersg)\
**Post date:** [May 29, 2020, 5:23pm UTC](https://community.hubspot.com/t/how-do-i-concatenate-strings-with-hubl/12876/5 "2020-05-29T17:23:18Z")

</div>

@BillOddie

I think a problem here may have been trying to access a variable that’s assigned a value in a for loop outside of said loop

So instead of

```auto
{% set items = "" %}
{% for row in hubdb_table_rows(1034058, "orderBy=random()") %}
 {{ items = items ~ ("Test"+loop.index0+", ") }}
{% endfor %}
<div>{{ items }}</div>

```

Where {{items}} has no value because you’re defining it and displaying it outside of the for loop which would assign it value.

I think you could do

```auto
{% set items = "" %}
{% for row in hubdb_table_rows(1034058, "orderBy=random()") %}
 {{ items = items ~ ("Test"+loop.index0+", ") }}
 <div>{{ items }}</div>
{% endfor %}  

```

---

<div class="post-metadata">

**Author:** ![JVidal7](https://avatars.discourse-cdn.com/v4/letter/j/e274bd/32.png) [@JVidal7](https://community.hubspot.com/u/JVidal7)\
**Post date:** [September 29, 2023, 3:29pm UTC](https://community.hubspot.com/t/how-do-i-concatenate-strings-with-hubl/12876/6 "2023-09-29T15:29:21Z")

</div>

Bless you Stefen! Where did you get this info? I didn’t find it in the HubL doc.

---

<div class="post-metadata">

**Author:** ![system](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/system/32/124_2.png) [@system](https://community.hubspot.com/u/system)\
**Post date:** [May 18, 2021, 9:53am UTC](https://community.hubspot.com/t/how-do-i-concatenate-strings-with-hubl/12876/7 "2021-05-18T09:53:18Z")

</div>

Hi @BillOddie ,  
Please try the below code:  
{% set str\_C = ‘’ %}  
\<ul\>  
\<li data-position=“{{ row.price }}” data-ID=“{{ c }}” class=“content domain-block project f-{{ row.sort\_by[0].id }} {% for new\_row in row.sort\_by %}{% if(str\_C != ‘’) %}{% set str\_C = str\_C ~ ’ ’ ~ new\_row.name|replace(’ ‘,’-‘) %}{% else %}{% set str\_C = ’ ’ ~ new\_row.name|replace(’ ‘,’-‘) %}{% endif %}{{str\_C|replace(’\_‘,’ ')}}{% endfor %}” data-filter=“f-{{ row.sort\_by[0].id }}”\>  
\</li\>  
\</ul\>ORPlease try this but not passed values out the for loop:  
{% set test\_varC = ‘’ %}  
{% macro render\_dialog(items,test\_varC) %} {% for x in items %}  
{% set test\_varC = test\_varC ~ x %}  
\<h1\>  
{{test\_varC}}  
\</h1\>  
{% endfor %}{% endmacro %}  
{% set items = [‘item 1’, ‘itme 2’, ‘item 3’] %}// Function call  
{% call render\_dialog(items,test\_varC) %}  
{% endcall %}  
Hope this helps!  
If we were able to answer your query, kindly help the community by marking it as a solution.  
Thanks and Regards.
