# Concatenate and string

**URL:** <https://discourse.nodered.org/t/concatenate-and-string/90342>\
**Category:** General\
**Created:** [22 August 2024 17:13 UTC](https://discourse.nodered.org/t/concatenate-and-string/90342 "2024-08-22T17:13:16Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![JonnyTech](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jonnytech/32/93759_2.png) [@JonnyTech](https://discourse.nodered.org/u/JonnyTech)\
**Post date:** [22 August 2024 17:13 UTC](https://discourse.nodered.org/t/concatenate-and-string/90342/1 "2024-08-22T17:13:16Z")

</div>

I have an environment variable named `ip-address` and can assign access it using `${ip-address}`.

For example I assign it to a dashboard text node label or use it in a change node to set a message field.

I use it because the rig I have moves around and it used with different networks so I change the variable and it is reflected on may nodes.

But I would like to be able to simplify matters even further.

If I set `ip-address` ro just the first three octets, ie `192.168.1.` then I can use it to change further nodes.

For example, I have three nodes that are set to `192.168.1.11`, `192.168.1.12`, `192.168.1.13`.

I could then assign something like `${ip-address} + "1"`, `${ip-address} + "2"`, `${ip-address} + "3"` to set them accordingly.

Rather than change 3 (or more) places, then I change just one.

Unfortunately I cannot figure out the syntax, the ` + "1"` does not work. Is it possible to do?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [22 August 2024 17:20 UTC](https://discourse.nodered.org/t/concatenate-and-string/90342/2 "2024-08-22T17:20:15Z")

</div>

As per [the docs](https://nodered.org/docs/user-guide/environment-variables):

> This only works if it replaces the entire property - it cannot be used to substitute just part of the value. For example, it is _not_ possible to use `CLIENT-${HOST}` .

However, if the field where you are using it is a TypedInput (where it has the `$` env selection) then it is possible e.g. `${ip-address}1`

  

The only other option is to use a change node, function node, or other node to do the string concatenation.

Example Function:

```auto
msg.ip = env.get('ip-address') + '1'
return msg

```

---

<div class="post-metadata">

**Author:** ![JonnyTech](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jonnytech/32/93759_2.png) [@JonnyTech](https://discourse.nodered.org/u/JonnyTech)\
**Post date:** [22 August 2024 17:32 UTC](https://discourse.nodered.org/t/concatenate-and-string/90342/3 "2024-08-22T17:32:31Z")

</div>

The change node appears to accept a typed input (see test1 below) but then adding suffixes, whether with no concatenate characters or a plus symbol does not work.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/4/442cc1ce1531829c9ac25612555ae7ce8869989a.png)

(Note that I am only assigning to a flow variable because it is very quick and easy to monitor the resulting values)

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [22 August 2024 17:43 UTC](https://discourse.nodered.org/t/concatenate-and-string/90342/4 "2024-08-22T17:43:43Z")

</div>

Try using the env type input

 ![chrome_screenshot_22 Aug 2024 18_41_44 BST](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/9/49c9ae2e002bccc29194b5cf4252bb6f1d979668.png)  
As Steve noted

> [@Steve-Mcl](#):
>
> TypedInput (where it has the `$` env selection)

---

<div class="post-metadata">

**Author:** ![JonnyTech](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jonnytech/32/93759_2.png) [@JonnyTech](https://discourse.nodered.org/u/JonnyTech)\
**Post date:** [22 August 2024 18:39 UTC](https://discourse.nodered.org/t/concatenate-and-string/90342/5 "2024-08-22T18:39:42Z")

</div>

> Try using the env type input

Thank you, that works in a change node perfectly.

How can I also make it work in a standard text field that accepts typed inputs?

I tried `${ip-address}` which is fine but `${ip-address}1` or even `${ip-address}${another-var}` both fail.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [22 August 2024 19:14 UTC](https://discourse.nodered.org/t/concatenate-and-string/90342/6 "2024-08-22T19:14:29Z")

</div>

> [@JonnyTech](#):
>
> How can I also make it work in a standard text field

You cannot

> [@Steve-Mcl](#):
>
> As per [the docs](https://nodered.org/docs/user-guide/environment-variables):
> 
> > This only works if it replaces the entire property - it cannot be used to substitute just part of the value. For example, it is _not_ possible to use `CLIENT-${HOST}` .

Instead, you would need to do it in a function or change node using JSONata

> [@Steve-Mcl](#):
>
> Example Function:
> 
> ```auto
> msg.ip = env.get('ip-address') + '1'
> return msg
> 
> ```

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [22 August 2024 19:20 UTC](https://discourse.nodered.org/t/concatenate-and-string/90342/7 "2024-08-22T19:20:09Z")

</div>

or  
in a template node

```auto
{{env.ip_prefix}}1

```

---

<div class="post-metadata">

**Author:** ![JonnyTech](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jonnytech/32/93759_2.png) [@JonnyTech](https://discourse.nodered.org/u/JonnyTech)\
**Post date:** [23 August 2024 11:19 UTC](https://discourse.nodered.org/t/concatenate-and-string/90342/8 "2024-08-23T11:19:34Z")

</div>

> As per the docs

Ah, I missed that!

So, I am trying another tactic, because it better suits my workflow (I get the details that way): an array of addresses, ie `["192.168.1.1","192.168.1.2","192.168.1.3"]` which I assign as a JSON string

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/e/ee2f2b715f466ab2f79a82578646e3c16d4737ae.png)

Trouble is trying to access the elements, `${ip-address[0]}` was my guess, which is incorrect. How far off is my syntax?

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [23 August 2024 11:43 UTC](https://discourse.nodered.org/t/concatenate-and-string/90342/9 "2024-08-23T11:43:03Z")

</div>

You would not us env input.  
You would use msg input  
`msg.``ip-address[0]`

If the json is in a env var, then first set it to a msg var.  
So would require two rules.  
set  
`msg.` `ìp-address`  
to  
`$` `ip-address`

then  
set  
`msg.` `ip-address`  
to  
`msg.` `ip-address[0]`

Or  
with one rule use JSONata input  
set  
`msg.` `ip-address`  
to  
`J:` `$env("ip-address")[0]`

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [6 September 2024 11:43 UTC](https://discourse.nodered.org/t/concatenate-and-string/90342/10 "2024-09-06T11:43:46Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
