# String cutting, in the middle

**URL:** <https://discourse.nodered.org/t/string-cutting-in-the-middle/80050>\
**Category:** General\
**Created:** [25 July 2023 07:42 UTC](https://discourse.nodered.org/t/string-cutting-in-the-middle/80050 "2023-07-25T07:42:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![gideon](https://avatars.discourse-cdn.com/v4/letter/g/7ba0ec/32.png) [@gideon](https://discourse.nodered.org/u/gideon)\
**Post date:** [25 July 2023 07:42 UTC](https://discourse.nodered.org/t/string-cutting-in-the-middle/80050/1 "2023-07-25T07:42:57Z")

</div>

Hello everyone.

On the Raspberry Pi I connect to the USB input a module that has 8 A/D inputs.  
The module sends to the serial port every 500 MS the reading of 8 channels in analog input.  
I need a function that will filter the channel I use let's say ch1  
Which function will do this?  
this is a sample reading "  
"CH1: 2004 \> 1.645V"  
"CH2: 2134 \> 0.640V"  
"CH3: 2304 \> 1.145V"  
''  
"CH8: 3304 \> 1.005V"  
the debug window show  
/25/2023, 8:54:43 AM[node: b15217b9f6a8af28](http://127.0.0.1:1880/#)msg.payload : string[17]

"CH6:2010→1.620V↵"

7/25/2023, 8:54:43 AM[node: b15217b9f6a8af28](http://127.0.0.1:1880/#)msg.payload : string[17]

string[17]

CH7:2184 1.759V

7/25/2023, 8:54:43 AM[node: b15217b9f6a8af28](http://127.0.0.1:1880/#)msg.payload : string[17]

"CH8:2010→1.619V↵"

I need to send just the "CH2: 0.640V"  
thanks

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [25 July 2023 08:38 UTC](https://discourse.nodered.org/t/string-cutting-in-the-middle/80050/2 "2023-07-25T08:38:06Z")

</div>

Your post title suggests that the readings arrive in Node-red as a single, multiple line string, but the debug outputs look like each channel is a separate message.

If it is a single multi-line string you can use a split node to split on new lines (I think that's the default for the node) which will give one message per line.

Then a switch node can select only those messages which contain "CH2"

---

<div class="post-metadata">

**Author:** ![gideon](https://avatars.discourse-cdn.com/v4/letter/g/7ba0ec/32.png) [@gideon](https://discourse.nodered.org/u/gideon)\
**Post date:** [25 July 2023 10:00 UTC](https://discourse.nodered.org/t/string-cutting-in-the-middle/80050/3 "2023-07-25T10:00:56Z")

</div>

I am receiving messages with /n for each.  
the lenegh of massage is 17.  
but I just need the start of the message and the end.  
I get this massage "CH1: 2004 \> 1.645V"  
and I want to change it to be look like that "CH1: 1.645V"

---

<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:** [25 July 2023 11:06 UTC](https://discourse.nodered.org/t/string-cutting-in-the-middle/80050/4 "2023-07-25T11:06:37Z")

</div>

> [@gideon](#):
>
> I want to change it to be look like that "CH1: 1.645V"

Are you sure that is what you want? What do you want to do with it?  
Possible a javascript object would be better, something like  
`{ch1: 1.645}`

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [25 July 2023 11:24 UTC](https://discourse.nodered.org/t/string-cutting-in-the-middle/80050/5 "2023-07-25T11:24:27Z")

</div>

![Untitled 1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/6/967aea72b92698f1d1b507ec2ad8b1dee25f37d0.jpeg)

```auto
[{"id":"e8198f13aa18b867","type":"inject","z":"67fbfce4b5946759","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"CH1: 2004 > 1.645V","payloadType":"str","x":390,"y":80,"wires":[["09913610d450badd","9d3afdf8a06f5275"]]},{"id":"c2fa3c699afc56ea","type":"debug","z":"67fbfce4b5946759","name":"Object","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":790,"y":80,"wires":[]},{"id":"09913610d450badd","type":"function","z":"67fbfce4b5946759","name":"Output as object","func":"const myarray = msg.payload.split(\" \")\nconst channel = myarray[0].split(\":\")[0]\nconst value = parseFloat(myarray[3])\nmsg.payload = {}\nmsg.payload[channel] = value\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":620,"y":80,"wires":[["c2fa3c699afc56ea"]]},{"id":"9d3afdf8a06f5275","type":"function","z":"67fbfce4b5946759","name":"Output as string","func":"const myarray = msg.payload.split(\" \")\nconst channel = myarray[0]\nconst value = myarray[3]\nmsg.payload = channel + \" \" + value\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":620,"y":140,"wires":[["3ed3ae26512a3eb6"]]},{"id":"3ed3ae26512a3eb6","type":"debug","z":"67fbfce4b5946759","name":"String","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":790,"y":140,"wires":[]}]

```

I tried at first to do it with neither function nodes nor Jsonata.  
While I can convert the input string to an array with split and join nodes, I don't know enough about change node syntax (dollar signs? curly brackets?) to rejoin array elements [0] and [3] in the desired format.

Edit:

 ![Untitled 2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/0/302675f6bb4641b667cbae74c1710aac80ec97fa.jpeg)

```auto
[{"id":"c3c8be7bb8325ba3","type":"inject","z":"67fbfce4b5946759","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"CH1: 2004 > 1.645V","payloadType":"str","x":290,"y":220,"wires":[["e3414a4622f30570"]]},{"id":"e3414a4622f30570","type":"split","z":"67fbfce4b5946759","name":"","splt":" ","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":450,"y":220,"wires":[["7344af38956f2bc8"]]},{"id":"9110bfa30f919d0f","type":"join","z":"67fbfce4b5946759","name":"","mode":"custom","build":"string","property":"payload","propertyType":"msg","key":"topic","joiner":" ","joinerType":"str","accumulate":false,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":690,"y":220,"wires":[["28e42d691b227f29"]]},{"id":"28e42d691b227f29","type":"debug","z":"67fbfce4b5946759","name":"String","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":810,"y":220,"wires":[]},{"id":"7344af38956f2bc8","type":"switch","z":"67fbfce4b5946759","name":"","property":"parts.index","propertyType":"msg","rules":[{"t":"eq","v":"0","vt":"num"},{"t":"eq","v":"3","vt":"num"}],"checkall":"true","repair":false,"outputs":2,"x":570,"y":220,"wires":[["9110bfa30f919d0f"],["9110bfa30f919d0f"]]}]

```

---

<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:** [23 September 2023 11:24 UTC](https://discourse.nodered.org/t/string-cutting-in-the-middle/80050/6 "2023-09-23T11:24:31Z")

</div>

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