# Function node to compare a msg.payload string

**URL:** <https://discourse.nodered.org/t/function-node-to-compare-a-msg-payload-string/35668>\
**Category:** General\
**Created:** [10 November 2020 12:09 UTC](https://discourse.nodered.org/t/function-node-to-compare-a-msg-payload-string/35668 "2020-11-10T12:09:16Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![leeg77](https://avatars.discourse-cdn.com/v4/letter/l/c5a1d2/32.png) [@leeg77](https://discourse.nodered.org/u/leeg77)\
**Post date:** [10 November 2020 12:09 UTC](https://discourse.nodered.org/t/function-node-to-compare-a-msg-payload-string/35668/1 "2020-11-10T12:09:17Z")

</div>

I am using the Tesseract node to read some text that I want to compare but it isn't working out.

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

In this case I want to compare the output of the Tesseract node which with the string "DEL"

This is my function node

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

This is the input and output to the function node  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/f/af109690c3c87842b5bc0ec869a7217c77ffeccb.png)

---

<div class="post-metadata">

**Author:** ![FiloCara](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/filocara/32/27473_2.png) [@FiloCara](https://discourse.nodered.org/u/FiloCara)\
**Post date:** [10 November 2020 14:05 UTC](https://discourse.nodered.org/t/function-node-to-compare-a-msg-payload-string/35668/2 "2020-11-10T14:05:34Z")

</div>

Your function seems to work perfectly actually.

The function compares _DEL_ with _DEL\n\n_ and, since the strings are not equals, your function set the `msg.payload = 0`.

One possible solution could be to remove any new line character in your payload coming from the Tesseract node and then compare it with the "DEL" string. Adding the following line of code before your IF statement should do the job.

```auto
payload = payload.replace(/\n/g, "")

```

---

<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:** [10 November 2020 19:24 UTC](https://discourse.nodered.org/t/function-node-to-compare-a-msg-payload-string/35668/3 "2020-11-10T19:24:49Z")

</div>

Or use the trim method  
`msg.payload = msg.payload.trim();`

> **[JavaScript String trim() Method](https://www.w3schools.com/jsref/jsref_trim_string.asp)**

---

<div class="post-metadata">

**Author:** ![leeg77](https://avatars.discourse-cdn.com/v4/letter/l/c5a1d2/32.png) [@leeg77](https://discourse.nodered.org/u/leeg77)\
**Post date:** [11 November 2020 09:09 UTC](https://discourse.nodered.org/t/function-node-to-compare-a-msg-payload-string/35668/4 "2020-11-11T09:09:54Z")

</div>

Thanks for the replies, I have tried both methods by adding a line before my IF statement but the result is still 0.

So I have added another function node before this one with a debug node so I can see what is going on.

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

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/4/d441aa46362584761a420bdff3d2e498360c2c31.png)

The output from this Function node gives me "DEL" but the IF Function node is still giving me a result of 0  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/0/00354c14fe6263c71da6f6e695d504fc1a7420d5.png)

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [11 November 2020 09:33 UTC](https://discourse.nodered.org/t/function-node-to-compare-a-msg-payload-string/35668/5 "2020-11-11T09:33:52Z")

</div>

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

Add `node.warn(msg.payload[0])` to the top of your function - what do you see in the debug sidebar?

---

<div class="post-metadata">

**Author:** ![leeg77](https://avatars.discourse-cdn.com/v4/letter/l/c5a1d2/32.png) [@leeg77](https://discourse.nodered.org/u/leeg77)\
**Post date:** [11 November 2020 09:50 UTC](https://discourse.nodered.org/t/function-node-to-compare-a-msg-payload-string/35668/6 "2020-11-11T09:50:09Z")

</div>

Thanks, I had just stumbled that by taking out the square brackets then it would check the whole string. So by playing around and for future reference the number in the bracket indicates the character in the string that is going to be compared. Is there any notation in that I can put in that bracket if I wanted to check say the first 3 characters of that string?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [11 November 2020 09:58 UTC](https://discourse.nodered.org/t/function-node-to-compare-a-msg-payload-string/35668/7 "2020-11-11T09:58:36Z")

</div>

Strings are essentially 0 based char arrays (so mystring[1] accesses the 2nd character)

> [@leeg77](#):
>
> Is there any notation in that I can put in that bracket if I wanted to check say the first 3 characters of that string?

String are objects with many built in functions - 1 of them being `substr`. Check out the extensive [online documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substr) which also has examples and a dynamic run-able demo where you can test things out & see what happens.

---

<div class="post-metadata">

**Author:** ![leeg77](https://avatars.discourse-cdn.com/v4/letter/l/c5a1d2/32.png) [@leeg77](https://discourse.nodered.org/u/leeg77)\
**Post date:** [11 November 2020 10:01 UTC](https://discourse.nodered.org/t/function-node-to-compare-a-msg-payload-string/35668/8 "2020-11-11T10:01:51Z")

</div>

Brilliant, thanks for your help

---

<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 2021 10:02 UTC](https://discourse.nodered.org/t/function-node-to-compare-a-msg-payload-string/35668/9 "2021-01-10T10:02:04Z")

</div>

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