I need help with Split String.

Hello, I’m a beginner and I would like to know how can I modify this code, I would like to make a split string for my data that I transfer from Mega to Esp8266 I have 1 string and 7 sensor data that sends from mega to esp8266 via serial port and between the data I used; to split it.

Code for Arduino
String transferdata = “”;

void loop{
transferdata = "";
transferdata += a;
transferdata += ";";
transferdata += b;
transferdata += ";";
transferdata += c;
transferdata += ";";
transferdata += d;
transferdata += ";";
transferdata += digitalRead(lr);
transferdata += ";";
transferdata += digitalRead(wr);
transferdata += ";";
transferdata += digitalRead(wr2);
Serial3.println(transferdata);
}

ESP8266 code (The code that I need help to modify)

String splitString(String data, char separator, int index){
     
     [color=red]int found = 0;
     int strIndex[] = { 0, -1};
     int maxIndex = data.length() -1;

     for (int i = 0; i <= maxIndex && found <= index; i++){
          
           if (data.charAt(i) == separator || i == maxindex) {
                 found++;
                 strIndex[0] = strIndex[1] + 1;
                 strIndex[1] = (i == maxIndex) ? i+1 : i;
 }

}
    return found > index ? data.substring(strIndex[0], strIndex[1]) : ""; 

}[/color]

Thank You everyone!

you don't need to build up the transferdata String to send it over, just print to Serial3 a,b,c,d with the separator in between. (consider start and end markers possibly to make parsing easier / more robust). You could also send the data in binary form.

I would suggest to study Serial Input Basics to handle the parsing


Please correct your post above and add code tags around your code:
[code]`` [color=blue]// your code is here[/color] ``[/code].

It should look like this:// your code is here
(Also press ctrl-T (PC) or cmd-T (Mac) in the IDE before copying to indent your code properly)

Use of the String class can lead to memory corruption and hard to diagnose bugs. See the Evils of Strings page.

The aforementioned serial input basics tutorial uses strings (null terminated character arrays). It is well worth studying.