# Get sub string from variable

**URL:** <https://community.homey.app/t/get-sub-string-from-variable/88024>\
**Category:** Developers\
**Created:** [August 26, 2023, 9:22am UTC](https://community.homey.app/t/get-sub-string-from-variable/88024 "2023-08-26T09:22:39Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rudolph\_Marks](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/rudolph_marks/32/22781_2.png) [@Rudolph\_Marks](https://community.homey.app/u/Rudolph_Marks)\
**Post date:** [August 26, 2023, 9:22am UTC](https://community.homey.app/t/get-sub-string-from-variable/88024/1 "2023-08-26T09:22:39Z")

</div>

I Recieved the string “Voeg 45 g pH- toe”. It’s saved in a variable called “PHMessage”

I want to save the numeric value (45) into a number variable

What is the best way to do this?

---

<div class="post-metadata">

**Author:** ![RonnyW](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/ronnyw/32/10879_2.png) [@RonnyW](https://community.homey.app/u/RonnyW)\
**Post date:** [August 26, 2023, 10:19am UTC](https://community.homey.app/t/get-sub-string-from-variable/88024/2 "2023-08-26T10:19:49Z")

</div>

Inside of an app or in a flow?

---

<div class="post-metadata">

**Author:** ![Rudolph\_Marks](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/rudolph_marks/32/22781_2.png) [@Rudolph\_Marks](https://community.homey.app/u/Rudolph_Marks)\
**Post date:** [August 26, 2023, 10:31am UTC](https://community.homey.app/t/get-sub-string-from-variable/88024/3 "2023-08-26T10:31:23Z")

</div>

Good question flow would be nice but a script is okay to because I run that first setting the value and use it afterwards

---

<div class="post-metadata">

**Author:** ![RonnyW](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/ronnyw/32/10879_2.png) [@RonnyW](https://community.homey.app/u/RonnyW)\
**Post date:** [August 26, 2023, 10:52am UTC](https://community.homey.app/t/get-sub-string-from-variable/88024/4 "2023-08-26T10:52:25Z")

</div>

You can use the HomeyScript flow card and pass the token with the string as input parameter.

The script can be like this:

return args[0].split(’ ')[1];

args is a input array.  
args[0] is your token value.  
The text is splitted ad blanks and the second element is returned.

---

<div class="post-metadata">

**Author:** ![Rudolph\_Marks](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/rudolph_marks/32/22781_2.png) [@Rudolph\_Marks](https://community.homey.app/u/Rudolph_Marks)\
**Post date:** [August 26, 2023, 11:50am UTC](https://community.homey.app/t/get-sub-string-from-variable/88024/5 "2023-08-26T11:50:28Z")

</div>

It’s not always on the same position, but always between to words/phrases

---

<div class="post-metadata">

**Author:** ![RonnyW](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/ronnyw/32/10879_2.png) [@RonnyW](https://community.homey.app/u/RonnyW)\
**Post date:** [August 26, 2023, 3:48pm UTC](https://community.homey.app/t/get-sub-string-from-variable/88024/6 "2023-08-26T15:48:02Z")

</div>

If it’s the only number then something like this:  
Just a simple loop through the split array. You can use forEach, too.

```auto
let array = args[0].split(’ ');
for (let i=0; i<array.length; i++){
  if (typeof array[i] == 'number'){
    return parseFloat(array[i]);
}
throw new Error('No number found');

```

Just written down and not tested 🙂

---

<div class="post-metadata">

**Author:** ![robertklep](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robertklep/32/160628_2.png) [@robertklep](https://community.homey.app/u/robertklep)\
**Post date:** [August 26, 2023, 4:19pm UTC](https://community.homey.app/t/get-sub-string-from-variable/88024/7 "2023-08-26T16:19:04Z")

</div>

This is what regular expressions are used for:

```auto
const str = args[0];
const match = str.match(/\d+/)[0];
if (match) {
  let num = Number(match[0]);
  ...
}

```

This will find the _first_ number in the string and doesn’t match negative or decimal numbers.

---

<div class="post-metadata">

**Author:** ![Andrew\_Whiteley](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/andrew_whiteley/32/79081_2.png) [@Andrew\_Whiteley](https://community.homey.app/u/Andrew_Whiteley)\
**Post date:** [September 26, 2024, 8:30am UTC](https://community.homey.app/t/get-sub-string-from-variable/88024/8 "2024-09-26T08:30:43Z")

</div>

RonnyW - this looks great, but when I try to add it to my HomeScript .split is not recognised. Do I need to install something else to my home to get these commands?

---

<div class="post-metadata">

**Author:** ![Andrew\_Whiteley](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/andrew_whiteley/32/79081_2.png) [@Andrew\_Whiteley](https://community.homey.app/u/Andrew_Whiteley)\
**Post date:** [September 26, 2024, 8:34am UTC](https://community.homey.app/t/get-sub-string-from-variable/88024/9 "2024-09-26T08:34:55Z")

</div>

Ignore me please - it is case sensitive and needs a capital ‘S’. Aplogies. I am learning as fast as I can!
