# Split string based on ;

**URL:** <https://discourse.nodered.org/t/split-string-based-on/55754>\
**Category:** General\
**Created:** [27 December 2021 15:35 UTC](https://discourse.nodered.org/t/split-string-based-on/55754 "2021-12-27T15:35:56Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![PiMo131](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/pimo131/32/34978_2.png) [@PiMo131](https://discourse.nodered.org/u/PiMo131)\
**Post date:** [27 December 2021 15:35 UTC](https://discourse.nodered.org/t/split-string-based-on/55754/1 "2021-12-27T15:35:56Z")

</div>

Hi,

I have a text file with location data when I use the 'file in' node i get strings with the format "A;B;C"

A,B,C don't have a consistent length. How can i split the string and return the value to

Msg.A  
Msg.B  
Msg.C  
Msg.payload (original payload)

Sample data:

L1S3;52.61563261441822;4.0658098986827715

L1S1;52.44174022895096;6.384215548118491

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [27 December 2021 15:43 UTC](https://discourse.nodered.org/t/split-string-based-on/55754/2 "2021-12-27T15:43:54Z")

</div>

Hello,

You can use the CSV node

**Demo Flow:**

```auto
[{"id":"b2dac34a6d1fcd85","type":"template","z":"54efb553244c241f","name":"data","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"L1S3;52.61563261441822;4.0658098986827715\nL1S1;52.44174022895096;6.384215548118491","output":"str","x":300,"y":1200,"wires":[["d76adacd90c1773f"]]},{"id":"df069b0fe53e1771","type":"inject","z":"54efb553244c241f","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":160,"y":1200,"wires":[["b2dac34a6d1fcd85"]]},{"id":"d76adacd90c1773f","type":"csv","z":"54efb553244c241f","name":"","sep":";","hdrin":"","hdrout":"none","multi":"mult","ret":"\\n","temp":"A, B, C","skip":"0","strings":true,"include_empty_strings":"","include_null_values":"","x":420,"y":1200,"wires":[["2694dcd575e2d741"]]},{"id":"2694dcd575e2d741","type":"debug","z":"54efb553244c241f","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":590,"y":1200,"wires":[]}]

```

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

---

<div class="post-metadata">

**Author:** ![tree-frog](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tree-frog/32/89303_2.png) [@tree-frog](https://discourse.nodered.org/u/tree-frog)\
**Post date:** [27 December 2021 15:49 UTC](https://discourse.nodered.org/t/split-string-based-on/55754/3 "2021-12-27T15:49:46Z")

</div>

The csv node is an excellent choice. Another option is,

node-red-contrib-string

This node has options to parse values to array elements. This should be the easiest way to do the most (string operations) without programming. You can also run multiple commands in succession to your input string(s).

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/2/7257896bccf3fb4e08ea4d2cc81dd83e0fe7e6ca.png)

---

<div class="post-metadata">

**Author:** ![tree-frog](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tree-frog/32/89303_2.png) [@tree-frog](https://discourse.nodered.org/u/tree-frog)\
**Post date:** [27 December 2021 16:52 UTC](https://discourse.nodered.org/t/split-string-based-on/55754/4 "2021-12-27T16:52:28Z")

</div>

To manage keeping your original payload value, you can simply use a "Change" node to shift/copy the payload to another element in your message. You can call it "original\_payload" or whatever you like. Then retrieve it by shifting it back to the payload after you manage the results of the split elements.

---

<div class="post-metadata">

**Author:** ![mickym2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mickym2/32/36128_2.png) [@mickym2](https://discourse.nodered.org/u/mickym2)\
**Post date:** [27 December 2021 19:08 UTC](https://discourse.nodered.org/t/split-string-based-on/55754/5 "2021-12-27T19:08:21Z")

</div>

An other solution and better for getting separate messages is using a split node.

---

<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:** [10 January 2022 19:08 UTC](https://discourse.nodered.org/t/split-string-based-on/55754/6 "2022-01-10T19:08:29Z")

</div>

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