# Substring() in function node does not slice

**URL:** <https://discourse.nodered.org/t/substring-in-function-node-does-not-slice/72886>\
**Category:** General\
**Tags:** function-node\
**Created:** [29 December 2022 11:36 UTC](https://discourse.nodered.org/t/substring-in-function-node-does-not-slice/72886 "2022-12-29T11:36:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ozett](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ozett/32/28862_2.png) [@ozett](https://discourse.nodered.org/u/ozett)\
**Post date:** [29 December 2022 11:36 UTC](https://discourse.nodered.org/t/substring-in-function-node-does-not-slice/72886/1 "2022-12-29T11:36:24Z")

</div>

first of all, i am no coder, but fiddling longtime with node-red.  
now i want a part of a string in a function-node,  
but get crazy results. to my impression substring() and substr() and slice() should cut out a part of the string, but all results are different.  
anybody with a hint for me?

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

can i post the flow in a horizontal way? seen that here around, but the hint doesnt help me to post is horizontal and smaller ([How to share code or flow json](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506))

better flow-show (edit):

```auto
[{"id":"c8801ef73e9aea5e","type":"inject","z":"e53ac3e112912e4f","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"#!1234567890abcdefgh","payloadType":"str","x":1300,"y":1120,"wires":[["1d7eef6c963d591b"]]},{"id":"1d7eef6c963d591b","type":"function","z":"e53ac3e112912e4f","name":"cast substring to hex","func":"var data = msg.payload\n\n//var sub1 = data.substring(8, 2);\n//msg.substring1 = sub1;\n\nvar substr = data.substr(8, 2);\nmsg.substring2 = substr;\n\nvar slice = data.slice(8, 2);\nmsg.slice = slice;\n\n\nvar sub1 = data.substring(8, 2);\nmsg.substring1 = sub1;\n\nreturn msg\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1580,"y":1120,"wires":[["8f97295851177c9e"]]},{"id":"8f97295851177c9e","type":"debug","z":"e53ac3e112912e4f","name":"subrintg","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"true","targetType":"full","statusVal":"payload","statusType":"auto","x":1800,"y":1120,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [29 December 2022 11:45 UTC](https://discourse.nodered.org/t/substring-in-function-node-does-not-slice/72886/2 "2022-12-29T11:45:22Z")

</div>

They are different because the way they operate is different.

- `substring` : you provide the start and end index  
[String.prototype.substring() - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring)
- `substr` : you provide the start and **length** [String.prototype.substr() - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substr)
- `slice` : functionally the same as `substring`, but some differences around handling negative indexes.

This post provides a good summary of the differences: [javascript - What is the difference between String.slice and String.substring? - Stack Overflow](https://stackoverflow.com/questions/2243824/what-is-the-difference-between-string-slice-and-string-substring)

Now, that all said, I cannot immediately see why your `slice` example is resulting in an empty string. Im not in a place to import your flow and test it for myself.

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [29 December 2022 11:51 UTC](https://discourse.nodered.org/t/substring-in-function-node-does-not-slice/72886/3 "2022-12-29T11:51:24Z")

</div>

> [@ozett](#):
>
> can i post the flow in a horizontal way? seen that here around,

Well I can answer that part of your question. Just click the 'compact' option when you export your flow.  
 ![compact](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/1/713d87bc64747b71a30295ad59d4d8b20feda57a.png)

---

<div class="post-metadata">

**Author:** ![ozett](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ozett/32/28862_2.png) [@ozett](https://discourse.nodered.org/u/ozett)\
**Post date:** [29 December 2022 12:45 UTC](https://discourse.nodered.org/t/substring-in-function-node-does-not-slice/72886/4 "2022-12-29T12:45:32Z")

</div>

thanks a ton, you helped me out.  
the slice lacked also my real understandig of an bigger count for the "END-index".  
i corrected the substring()-indices, that worked.  
and used the same index-values from the substring() for the slice(). that worked to.

no i am a real programmer. 🤣🤣🤣  
big thanks again, for all.

---

<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:** [12 January 2023 12:46 UTC](https://discourse.nodered.org/t/substring-in-function-node-does-not-slice/72886/5 "2023-01-12T12:46:03Z")

</div>

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