# String compare not working when string contains ()

**URL:** <https://discourse.nodered.org/t/string-compare-not-working-when-string-contains/71147>\
**Category:** General\
**Created:** [23 November 2022 22:46 UTC](https://discourse.nodered.org/t/string-compare-not-working-when-string-contains/71147 "2022-11-23T22:46:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![gobigdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gobigdave/32/71265_2.png) [@gobigdave](https://discourse.nodered.org/u/gobigdave)\
**Post date:** [23 November 2022 22:46 UTC](https://discourse.nodered.org/t/string-compare-not-working-when-string-contains/71147/1 "2022-11-23T22:46:00Z")

</div>

I have a msg payload where:

msg.payload = "(INP 8)"

 ![Screenshot 2022-11-23 at 5.34.45 PM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/6/36301339377c91a53b1aadea148c40a8e0209441.png)

The switch node is a simple:

msg.property != "(INP 8)" --\> 1

Even though msg.payload is "(INP 8)", the switch node sends the message on. I tried the same thing with state on the Home Assistant node, and it never did the comparison correctly there either.

Here is the debug output showing the value of msg.payload.  
 ![Screenshot 2022-11-23 at 5.38.02 PM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/3/d3ffb15c1b33ce4346e3a06d6236b62506cb82e4.png)

What gives? It's just a simple string. I do string caparisons in switches elsewhere without a problem. Same for state.

For grins, I setup an inject node to send msg.payload = "(INP 8)". With that, it works. My guess now is there are either hidden characters in the state I'm receiving from my device, or there is a character set issue. How can I see that?

---

<div class="post-metadata">

**Author:** ![gobigdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gobigdave/32/71265_2.png) [@gobigdave](https://discourse.nodered.org/u/gobigdave)\
**Post date:** [23 November 2022 22:55 UTC](https://discourse.nodered.org/t/string-compare-not-working-when-string-contains/71147/2 "2022-11-23T22:55:31Z")

</div>

I was able to make it work by adding a function node:

msg.payload = msg.payload.replace(/[^a-zA-Z0-9()]/g, "")  
return msg;

Must be some hidden characters in there. My question still stands. How would I see those since nothing is showing in the debug window.

---

<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:** [23 November 2022 23:13 UTC](https://discourse.nodered.org/t/string-compare-not-working-when-string-contains/71147/3 "2022-11-23T23:13:12Z")

</div>

If I just wanted to check that particular string, I'd send it to a Write file node and then display the contents with **od -bc filename** (print as octal chars and printable/escape chars)

---

<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:** [23 November 2022 23:14 UTC](https://discourse.nodered.org/t/string-compare-not-working-when-string-contains/71147/4 "2022-11-23T23:14:14Z")

</div>

They must be unprintable control characters.

One clue in the debug side was the `string[50]` - telling you it believed the string to be 50 characters long.

We do replace some control characters to make them more obvious, such as newline and tab. It may be there's more to be done.

---

<div class="post-metadata">

**Author:** ![gobigdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gobigdave/32/71265_2.png) [@gobigdave](https://discourse.nodered.org/u/gobigdave)\
**Post date:** [24 November 2022 00:21 UTC](https://discourse.nodered.org/t/string-compare-not-working-when-string-contains/71147/5 "2022-11-24T00:21:52Z")

</div>

That’s what I figured. It was a bit of a head scratcher, but now that I have the function node removing any special characters all is good.

---

<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 January 2023 00:22 UTC](https://discourse.nodered.org/t/string-compare-not-working-when-string-contains/71147/6 "2023-01-23T00:22:11Z")

</div>

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