Substring() in function node does not slice

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?

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)

better flow-show (edit):

[{"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":[]}]

They are different because the way they operate is different.

This post provides a good summary of the differences: javascript - What is the difference between String.slice and String.substring? - Stack Overflow

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.

Well I can answer that part of your question. Just click the 'compact' option when you export your flow.

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. :rofl: :rofl: :rofl:
big thanks again, for all.

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