# virtualWire String subStrings problems /!\\

**URL:** <https://forum.arduino.cc/t/virtualwire-string-substrings-problems/525265>\
**Category:** Programming\
**Created:** [May 10, 2018, 8:06am UTC](https://forum.arduino.cc/t/virtualwire-string-substrings-problems/525265 "2018-05-10T08:06:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marsat](https://avatars.discourse-cdn.com/v4/letter/m/e68b1a/32.png) [@Marsat](https://forum.arduino.cc/u/Marsat)\
**Post date:** [May 10, 2018, 8:06am UTC](https://forum.arduino.cc/t/virtualwire-string-substrings-problems/525265/1 "2018-05-10T08:06:10Z")

</div>

Hi, nice to meet you !

I'm currently building a transmitter with VirtuaWire for a little submarine Drone, i want to tell him for example "for this period of time you will go up" and some other stuff. To do that i use some radio emitter-receiver (low frequence usualy work underwater, as long i'm not too deep).

But i have a problem: when you send the message, you do not send a simple String, it is more a char buffer with the size of a String or something right?

So when i receive the message i want to do this for example (this is in loop):

```cpp
if (coms.substring(3) == "M1r" && mode == 1) {
    sayM("M1r reçus", MOTOR1); // Detected but never works
    nombres = coms.substring(5, 8); //I'm transfering the number to an other String to be sure
    M1r = nombres.toInt(); //Motor will go "on" for this amount of time
    sayM("COMS recus = ", COMS); //Finish the lines so i know everything went fine
  }

```

coms is the string that i receive from this in my virtualWire fonction :

I've got this before my setup:

```cpp
typedef struct {
  String commande;
  int valeur;
} MaStructure;

```

```cpp
  MaStructure message;
  byte taille_message = sizeof(MaStructure);
  
  vw_wait_rx_max(5000);
  //vw_wait_rx();

  if (vw_get_message((byte *) &message, &taille_message)) {
    radioToComs = message.commande; //I simply put the string from the message into a string more stable
  }

```

I know this fonction is working because i try some stuff like:

```cpp
if (coms == "bonjour"){
Serial.println("hello world");
}

```

But when i use the substring, nothing is working correctly... so i think it is a little bit tricky no?

If you have any thinking about that, i would be happy to here them 🙂

---

<div class="post-metadata">

**Author:** ![Marsat](https://avatars.discourse-cdn.com/v4/letter/m/e68b1a/32.png) [@Marsat](https://forum.arduino.cc/u/Marsat)\
**Post date:** [May 10, 2018, 8:21am UTC](https://forum.arduino.cc/t/virtualwire-string-substrings-problems/525265/2 "2018-05-10T08:21:23Z")

</div>

Yes, i forgot: i have this before my setup:

typedef struct {  
String commande;  
int valeur;  
} MaStructure;

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 10, 2018, 9:29am UTC](https://forum.arduino.cc/t/virtualwire-string-substrings-problems/525265/3 "2018-05-10T09:29:33Z")

</div>

> But i have a problem: when you send the message, you do not send a simple String, it is more a char buffer with the size of a String or something right?

Wrong. Forget that the stupid String class exists. What you send, and receive, is an array of bytes.

A byte is the same size as a char, so you could think of sending and receiving arrays of chars. But an array of chars is NOT a String, and does not have a substring method.

You could make the array of chars into a string (which is NOT the same thing as a String), by adding a NULL terminator in the proper position in the array. That's why the number of elements in the array is required on the sending end and provided on the receiving end.

Do NOT use Strings in your code. Learn to use strings, instead.

---

<div class="post-metadata">

**Author:** ![Marsat](https://avatars.discourse-cdn.com/v4/letter/m/e68b1a/32.png) [@Marsat](https://forum.arduino.cc/u/Marsat)\
**Post date:** [May 10, 2018, 4:56pm UTC](https://forum.arduino.cc/t/virtualwire-string-substrings-problems/525265/4 "2018-05-10T16:56:00Z")

</div>

Okay i see... more like this:  
[string - Arduino Reference](https://www.arduino.cc/reference/en/language/variables/data-types/string/) ?

---

<div class="post-metadata">

**Author:** ![Robin2](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/robin2/32/216045_2.png) [@Robin2](https://forum.arduino.cc/u/Robin2)\
**Post date:** [May 10, 2018, 5:37pm UTC](https://forum.arduino.cc/t/virtualwire-string-substrings-problems/525265/5 "2018-05-10T17:37:07Z")

</div>

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use [cstrings](http://www.cplusplus.com/reference/cstring/) - char arrays terminated with 0.

You will also make life easier for yourself if you make the messages as short as possible. Just using a single alphanumeric character gives you 62 options.

...R

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 5, 2021, 11:10pm UTC](https://forum.arduino.cc/t/virtualwire-string-substrings-problems/525265/6 "2021-05-05T23:10:57Z")

</div>


