# Read and write value from string

**URL:** https://discourse.nodered.org/t/read-and-write-value-from-string/9468
**Category:** General
**Created:** [26 March 2019 21:04 UTC](https://discourse.nodered.org/t/read-and-write-value-from-string/9468 "2019-03-26T21:04:42Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![smort](https://avatars.discourse-cdn.com/v4/letter/s/e5b9ba/32.png) [@smort](https://discourse.nodered.org/u/smort)
#### Post date: [26 March 2019 21:04 UTC](https://discourse.nodered.org/t/read-and-write-value-from-string/9468/1 "2019-03-26T21:04:43Z")

</div>

Hello I am trying to use the function node to modify a message from the TCP in node. I am trying to interface with a controller propitary interface. I have yet only managed to send a single value to the controller. Thats done by replying "?100=16" to the message "?100" where 16 is the value.

20:33:36.265: Tx: ?100  
20:33:36.312: Rx: ?100=16

When trying to send and recieve more than one value I don't have the skills to read the message in the function node and pass the correct reply. For instance I want to assign 100 in the "!14,7,100" to a variable and reply ?14=100 to confirm to the controller that the value is recieved, wrapped up like the Rx line below:

20:29:53.295: Tx: !12,1,0,0!14,7,100?14?16  
20:29:53.327: Rx: !12,1,0,0!14,7,100?14=100?16=23

The TCP in node is configured as stream of string and each "command group" in the string is separated by ! or ?.

Anyone who can push me in the right direction?

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [26 March 2019 21:27 UTC](https://discourse.nodered.org/t/read-and-write-value-from-string/9468/2 "2019-03-26T21:27:48Z")

</div>

To try and get to the crux of what you are trying to do, is it right that you have a value in a variable (which might be 100) and you want to build the string "!14,7," followed by the value of that variable? If so then you can just do  
`answer = "!14,7," + theVariable`  
The fact that the first expression on the right hand side is a string will tell javascript to convert `theVariable` to a string and concatenate it with the first part.

---

<div class="post-metadata">

### Author: ![smort](https://avatars.discourse-cdn.com/v4/letter/s/e5b9ba/32.png) [@smort](https://discourse.nodered.org/u/smort)
#### Post date: [26 March 2019 21:36 UTC](https://discourse.nodered.org/t/read-and-write-value-from-string/9468/3 "2019-03-26T21:36:33Z")

</div>

Thank you that worked for building the string. Now how do I read out "100" from !14,7,100 and stores it as a variable?

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [27 March 2019 08:19 UTC](https://discourse.nodered.org/t/read-and-write-value-from-string/9468/4 "2019-03-27T08:19:56Z")

</div>

> [@smort](#):
>
> how do I read out "100" from !14,7,100 and stores it as a variable?

Assuming that the commas are predictable then I would use the javascript split() function to split it at commas and then pick up the third element.

---

<div class="post-metadata">

### Author: ![smort](https://avatars.discourse-cdn.com/v4/letter/s/e5b9ba/32.png) [@smort](https://discourse.nodered.org/u/smort)
#### Post date: [28 March 2019 09:26 UTC](https://discourse.nodered.org/t/read-and-write-value-from-string/9468/5 "2019-03-28T09:26:07Z")

</div>

Thanks I ended up using the slice function to slice out the parts with ! and ? in front and then used a split node to split it further down and then used the msg.parts.index to read out the different values. [This page](https://www.w3schools.com/js/js_string_methods.asp) described everything I had to do with javascript glad I found it.
