# Command.substring(); command use

**URL:** <https://community.particle.io/t/command-substring-command-use/30869>\
**Category:** General\
**Created:** [March 23, 2017, 11:26am UTC](https://community.particle.io/t/command-substring-command-use/30869 "2017-03-23T11:26:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![imPawan](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/impawan/32/13727_2.png) [@imPawan](https://community.particle.io/u/imPawan)\
**Post date:** [March 23, 2017, 11:26am UTC](https://community.particle.io/t/command-substring-command-use/30869/1 "2017-03-23T11:26:46Z")

</div>

Particle photon the best board i ever worked on. I have controlled devices and motor for a robot using some of the command like command.substring(); but dont know what exactly doing. Can any one please explaint me how it is working.

```cpp
int ledControl(String command) {
    int state =0;
    int pinNumber = (command.charAt(1)-'0') - 1;
    if(pinNumber < 0 || pinNumber > 7) return -1;
    if(command.substring(3,7) == "HIGH") state = 1;
    else if(command.substring(3,6) == "LOW") state = 0;
    else return -2;
    
    digitalWrite(pinNumber,state);
    return 1;
} 

```

Thank you in advance please help.

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [March 23, 2017, 11:40am UTC](https://community.particle.io/t/command-substring-command-use/30869/2 "2017-03-23T11:40:45Z")

</div>

A quick search in the Particle docs would bring you here  
[https://docs.particle.io/reference/firmware/photon/#substring-](https://docs.particle.io/reference/firmware/photon/#substring-)

In short, you take the part of the `command` string (=sub-string) between the two string positions to work with.

---

<div class="post-metadata">

**Author:** ![imPawan](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/impawan/32/13727_2.png) [@imPawan](https://community.particle.io/u/imPawan)\
**Post date:** [March 23, 2017, 1:15pm UTC](https://community.particle.io/t/command-substring-command-use/30869/3 "2017-03-23T13:15:58Z")

</div>

Hey @ScruffR thank you for a quick reply. Still i am not able to understand why i am passing 3,7 and 3,6 in the above code.  
if(command.substring(3,7) == “HIGH”) state = 1;  
else if(command.substring(3,6) == “LOW”) state = 0;

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [March 23, 2017, 3:34pm UTC](https://community.particle.io/t/command-substring-command-use/30869/4 "2017-03-23T15:34:13Z")

</div>

Because the command string will be `D1=HIGH` or `D1=LOW`  
So count the string positions 3~7 for the first and 3~6 for the second …

… clearer?

---

<div class="post-metadata">

**Author:** ![imPawan](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/impawan/32/13727_2.png) [@imPawan](https://community.particle.io/u/imPawan)\
**Post date:** [March 24, 2017, 4:14am UTC](https://community.particle.io/t/command-substring-command-use/30869/5 "2017-03-24T04:14:08Z")

</div>

thanks @ScruffR 😄 got the point 😂
