split string present received in serial

Hi all
i have a short problem;
i’m receiving a string by another microcontrollor, for example:

sensor: 2063; Lat: 10.791688; Long: 2.600756; Alt: 296.00;

i have memorized this value in a string named “packet”

now i need to split a string in substring, for example:

sensor: 2063;
Lat: 10.791688;
Long: 2.600756;
Alt: 296.00;

i can split searching “;” character into the original string.

i’m try using following code example, but it doesn’t work;
could you help me please?

int pos[3];
int cont = 0;

for(int i=0; i<packet.length(); i++)
{
pos [cont] = packet.indexOf(";",i);
i = pos [cont];

}

Serial.println(pos[0]);
Serial.println(pos[1]);
Serial.println(pos[2]);
Serial.println(pos[3]);
Serial.println(pos[4]);

thank you

Welcome to the forums. Please read the sticky post at the top of the page about how to properly post your code inside code tags. It helps people help you.

As for your issue, you do not have a string, you have a String (capital S). String is a class that should be avoided. Learn how to use regular C strings instead.

Then, you can use strtok() to break up the string into pieces.

Your current code just finds the beginning of the next string but does not increment cont so it continues to write the index into pos[0].

You also only decare pos to be 3 elements yet print out 5 elements.

int pos[10];
int cont = 0;

void setup() {
  Serial.begin(9600);
  String packet = "sensor: 2063; Lat:  10.791688; Long:  2.600756; Alt:  296.00;";

  for (int i = 0; i < packet.length(); i++)
  {
    pos[cont] = packet.indexOf(";", i);
    i = pos[cont];
    cont++;
  }


  Serial.println(pos[0]);
  Serial.println(pos[1]);
  Serial.println(pos[2]);
  Serial.println(pos[3]);
}

void loop() {
  
}