# How to extract a string value

**URL:** <https://discourse.nodered.org/t/how-to-extract-a-string-value/15020>\
**Category:** General\
**Created:** [2 September 2019 08:00 UTC](https://discourse.nodered.org/t/how-to-extract-a-string-value/15020 "2019-09-02T08:00:04Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![mike829](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mike829/32/11676_2.png) [@mike829](https://discourse.nodered.org/u/mike829)\
**Post date:** [2 September 2019 08:00 UTC](https://discourse.nodered.org/t/how-to-extract-a-string-value/15020/1 "2019-09-02T08:00:04Z")

</div>

Hello Everybody,

I am working on a RS485 serial to ifttt project.

I receive the string "INPUTSTATUS 0924400005454010004411444100444111447855" and I would like to extract only the second number, in this example "9".

I am using the palette "string" but I don't know how to setup it to extract the value.

Can you please help me?

---

<div class="post-metadata">

**Author:** ![guysqr](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/guysqr/32/11551_2.png) [@guysqr](https://discourse.nodered.org/u/guysqr)\
**Post date:** [2 September 2019 08:13 UTC](https://discourse.nodered.org/t/how-to-extract-a-string-value/15020/2 "2019-09-02T08:13:37Z")

</div>

I'd use a function node. Assuming the data is in msg.payload:

```auto
let number = msg.payload.split(' ')[1].substr(1,1)
```

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [2 September 2019 08:32 UTC](https://discourse.nodered.org/t/how-to-extract-a-string-value/15020/3 "2019-09-02T08:32:34Z")

</div>

you probably mean (to be complete for mike829)

```auto
msg.payload = msg.payload.split(' ')[1].substr(1,1);
return msg;

```

---

<div class="post-metadata">

**Author:** ![guysqr](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/guysqr/32/11551_2.png) [@guysqr](https://discourse.nodered.org/u/guysqr)\
**Post date:** [2 September 2019 08:41 UTC](https://discourse.nodered.org/t/how-to-extract-a-string-value/15020/4 "2019-09-02T08:41:45Z")

</div>

Yes, should have documented better. 🙂 Thanks @dceejay.

---

<div class="post-metadata">

**Author:** ![mike829](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mike829/32/11676_2.png) [@mike829](https://discourse.nodered.org/u/mike829)\
**Post date:** [2 September 2019 09:39 UTC](https://discourse.nodered.org/t/how-to-extract-a-string-value/15020/5 "2019-09-02T09:39:28Z")

</div>

Thank you so much, so if i put substr(0,1); I read the first value, if i put substr(1,1); I read the second value, if I put substr(2,1); I will read the third value. Is it correct?

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [2 September 2019 09:57 UTC](https://discourse.nodered.org/t/how-to-extract-a-string-value/15020/6 "2019-09-02T09:57:41Z")

</div>

Correct..

---

<div class="post-metadata">

**Author:** ![mike829](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mike829/32/11676_2.png) [@mike829](https://discourse.nodered.org/u/mike829)\
**Post date:** [2 September 2019 10:01 UTC](https://discourse.nodered.org/t/how-to-extract-a-string-value/15020/7 "2019-09-02T10:01:04Z")

</div>

Thank you so much for your help. It works fine...

---

<div class="post-metadata">

**Author:** ![mike829](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mike829/32/11676_2.png) [@mike829](https://discourse.nodered.org/u/mike829)\
**Post date:** [2 September 2019 11:17 UTC](https://discourse.nodered.org/t/how-to-extract-a-string-value/15020/8 "2019-09-02T11:17:10Z")

</div>

Sorry again...Is it also possible to convert the hex I've extract to binary? If it is 9 I would like to obtain 1001.  
So I will have

1 \> state input 1 on  
0 \> state input 2 off  
0 \> state input 3 off  
1 \> state input 4 on

Now I am using the code in hex: If it is 9\> 4 and 1 are on. If it is 1 \> 1 is on. If it is 8 \> 4 is on. If it is 3 \> 1 and 2 are on.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [2 September 2019 15:15 UTC](https://discourse.nodered.org/t/how-to-extract-a-string-value/15020/9 "2019-09-02T15:15:16Z")

</div>

Maybe this would help?

> <https://stackoverflow.com/questions/1337419/how-do-you-convert-numbers-between-different-bases-in-javascript>

---

<div class="post-metadata">

**Author:** ![mike829](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mike829/32/11676_2.png) [@mike829](https://discourse.nodered.org/u/mike829)\
**Post date:** [2 September 2019 15:42 UTC](https://discourse.nodered.org/t/how-to-extract-a-string-value/15020/10 "2019-09-02T15:42:37Z")

</div>

Thanks for the link. I will check into it

---

<div class="post-metadata">

**Author:** ![mike829](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mike829/32/11676_2.png) [@mike829](https://discourse.nodered.org/u/mike829)\
**Post date:** [2 September 2019 15:47 UTC](https://discourse.nodered.org/t/how-to-extract-a-string-value/15020/11 "2019-09-02T15:47:31Z")

</div>

now I am trying to extract the string: { var: "5555 INFO 1" } to obtain only 5555 INFO 1.

I edit the function '' msg.payload = msg.payload.split(" ")[1].substr(5,10);  
return msg;'''

but doesn't work...

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [2 September 2019 15:52 UTC](https://discourse.nodered.org/t/how-to-extract-a-string-value/15020/12 "2019-09-02T15:52:59Z")

</div>

That’s not a string. msg.payload in that example is an object.  
Have you read the page “ working with messages” in the docs? Which would help you to differentiate them.  
the bit you want is msg.payload.var

---

<div class="post-metadata">

**Author:** ![mike829](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mike829/32/11676_2.png) [@mike829](https://discourse.nodered.org/u/mike829)\
**Post date:** [2 September 2019 16:31 UTC](https://discourse.nodered.org/t/how-to-extract-a-string-value/15020/13 "2019-09-02T16:31:53Z")

</div>

I will look into “ working with messages”, thank you. I think that I can use string palette and chomp. Thank you so much.

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [2 September 2019 17:42 UTC](https://discourse.nodered.org/t/how-to-extract-a-string-value/15020/14 "2019-09-02T17:42:37Z")

</div>

> [@mike829](#):
>
> think taht I can use string palette and chomp

or as i said before just use msg.payload.var

---

<div class="post-metadata">

**Author:** ![mike829](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mike829/32/11676_2.png) [@mike829](https://discourse.nodered.org/u/mike829)\
**Post date:** [3 September 2019 10:30 UTC](https://discourse.nodered.org/t/how-to-extract-a-string-value/15020/15 "2019-09-03T10:30:54Z")

</div>

It works using mg.payload.var and it is perfect to use chomp function to cut var: " and the final". I obtained the perfect string at the output. So thank you so much for the help.
