# Splitting and Assigning String Data

**URL:** <https://discourse.nodered.org/t/splitting-and-assigning-string-data/29426>\
**Category:** General\
**Created:** [3 July 2020 06:22 UTC](https://discourse.nodered.org/t/splitting-and-assigning-string-data/29426 "2020-07-03T06:22:18Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![shaunchester](https://avatars.discourse-cdn.com/v4/letter/s/77aa72/32.png) [@shaunchester](https://discourse.nodered.org/u/shaunchester)\
**Post date:** [3 July 2020 06:22 UTC](https://discourse.nodered.org/t/splitting-and-assigning-string-data/29426/1 "2020-07-03T06:22:19Z")

</div>

Hi,

I am using the Serial Node to Read Data from a serial USB device, it returns the following string:  
z+2.524+2.280+2.109+0.000  
Can anyone point me int he right direction of how to:  
Remove the 'z' and then split each value out and assign each value with a seperate 'channel' so I can distinguish between each channel from my device.

Thanks

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [3 July 2020 06:55 UTC](https://discourse.nodered.org/t/splitting-and-assigning-string-data/29426/2 "2020-07-03T06:55:49Z")

</div>

> [@shaunchester](#):
>
> z+2.524+2.280+2.109+0.000

How about this as a starting point?

```auto
[{"id":"9948412e.7102c8","type":"inject","z":"b9924a74.4d98f8","name":"","topic":"","payload":"z+2.524+2.280+2.109+0.000","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":4470,"wires":[["8c09fe88.a41d4"]]},{"id":"8c09fe88.a41d4","type":"split","z":"b9924a74.4d98f8","name":"","splt":"+","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":260,"y":4470,"wires":[["d2512309.42fb8","63d52c69.73181c"]]},{"id":"d2512309.42fb8","type":"debug","z":"b9924a74.4d98f8","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":260,"y":4420,"wires":[]},{"id":"63d52c69.73181c","type":"switch","z":"b9924a74.4d98f8","name":"","property":"parts.index","propertyType":"msg","rules":[{"t":"eq","v":"1","vt":"num"},{"t":"eq","v":"2","vt":"num"},{"t":"eq","v":"3","vt":"num"},{"t":"eq","v":"4","vt":"num"},{"t":"eq","v":"5","vt":"num"}],"checkall":"true","repair":false,"outputs":5,"x":410,"y":4470,"wires":[["8a6ffe84.0403b8"],["4f6ebec3.095e58"],["9d02e6fc.3ab27"],["af01a655.f963a"],["9173b25c.e3be4"]]},{"id":"8a6ffe84.0403b8","type":"debug","z":"b9924a74.4d98f8","name":"1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":560,"y":4390,"wires":[]},{"id":"4f6ebec3.095e58","type":"debug","z":"b9924a74.4d98f8","name":"2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":560,"y":4430,"wires":[]},{"id":"9d02e6fc.3ab27","type":"debug","z":"b9924a74.4d98f8","name":"3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":560,"y":4470,"wires":[]},{"id":"af01a655.f963a","type":"debug","z":"b9924a74.4d98f8","name":"4","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":560,"y":4510,"wires":[]},{"id":"9173b25c.e3be4","type":"debug","z":"b9924a74.4d98f8","name":"5","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":560,"y":4550,"wires":[]}]

```

I went over the top with outputs, but it will show you how to expand on things if you need more.

If you don't, just edit the `switch` node and delete the last output.

---

<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:** [3 July 2020 10:11 UTC](https://discourse.nodered.org/t/splitting-and-assigning-string-data/29426/3 "2020-07-03T10:11:21Z")

</div>

I would probably use a function node to convert it to a javascript object, something like this

```auto
const splitted = msg.payload.split("+")
msg.payload = {field1: splitted[1], field2: splitted[2], field3: splitted[3], field4: splitted[4]}
return msg;

```

Obviously change field1 etc to appropriate names.

```auto
[{"id":"46ff6251.23cd64","type":"inject","z":"bdd7be38.d3b55","name":"","props":[{"p":"payload","v":"z+2.524+2.280+2.109+0.000","vt":"str"},{"p":"topic","v":"","vt":"string"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"z+2.524+2.280+2.109+0.000","payloadType":"str","x":110,"y":1000,"wires":[["52cffe47.f91248"]]},{"id":"52cffe47.f91248","type":"function","z":"bdd7be38.d3b55","name":"","func":"const splitted = msg.payload.split(\"+\")\nmsg.payload = {field1: splitted[1], field2: splitted[2], field3: splitted[3], field4: splitted[4]}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":280,"y":1000,"wires":[["46b3ee82.308e88"]]},{"id":"46b3ee82.308e88","type":"debug","z":"bdd7be38.d3b55","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":470,"y":1000,"wires":[]}]

```

---

<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:** [3 July 2020 10:58 UTC](https://discourse.nodered.org/t/splitting-and-assigning-string-data/29426/4 "2020-07-03T10:58:18Z")

</div>

I would also do it in a function node.

```auto
msg.payload = msg.payload.split("+").slice(1);

```

edit/ did not catch the seperate channel part .

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [3 July 2020 13:33 UTC](https://discourse.nodered.org/t/splitting-and-assigning-string-data/29426/5 "2020-07-03T13:33:50Z")

</div>

You could also try a csv node set to split on +

---

<div class="post-metadata">

**Author:** ![shaunchester](https://avatars.discourse-cdn.com/v4/letter/s/77aa72/32.png) [@shaunchester](https://discourse.nodered.org/u/shaunchester)\
**Post date:** [4 July 2020 05:07 UTC](https://discourse.nodered.org/t/splitting-and-assigning-string-data/29426/6 "2020-07-04T05:07:30Z")

</div>

Cheers, I have implemented your solution and it works perfect!

Thanks for your time to write a response and flow.

---

<div class="post-metadata">

**Author:** ![shaunchester](https://avatars.discourse-cdn.com/v4/letter/s/77aa72/32.png) [@shaunchester](https://discourse.nodered.org/u/shaunchester)\
**Post date:** [4 July 2020 05:07 UTC](https://discourse.nodered.org/t/splitting-and-assigning-string-data/29426/7 "2020-07-04T05:07:50Z")

</div>

Cheers Colin, much appreciated

---

<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:** [18 July 2020 05:07 UTC](https://discourse.nodered.org/t/splitting-and-assigning-string-data/29426/8 "2020-07-18T05:07:53Z")

</div>

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