Clicky

20210204

Arduino/ESP Split function

I created the next string "Split" function for Arduino/ESP boards. The performance is OK (25ms/Split)


void setup() {
 
  Serial.begin(115200);
  delay(1000);

  Serial.println("*** Start...");

  String s = "1612248989;Random string with SPACES;192.168.2.2;192.168.3.3;64;16;ABCDE2345678987BCEF";
  Serial.println(s);


  Serial.println(Split(s,";",2));
 
  Serial.println("*** End.");
   
}

void loop() {
  // put your main code here, to run repeatedly:
}


String Split(String aString, String Delimiter, int Index) {
 
  String bString ="", fragment = "";
  int delimiterIndex, i = 0;
 
  bString = aString;  
    
  do {
    i++;
    delimiterIndex = bString.indexOf(Delimiter);
    if (i == Index) {
        fragment = bString.substring(0,delimiterIndex);
    }
    bString = bString.substring(delimiterIndex+1);
    bString.trim();   
  } while (delimiterIndex != -1);
  return fragment;

Result:

18:24:44.626 -> *** Start...
18:24:44.626 -> 1612248989;Random string with SPACES;192.168.2.2;192.168.3.3;64;16;ABCDE2345678987BCEF
18:24:44.626 -> Random string with SPACES
18:24:44.626 -> *** End.
 

No comments :

Post a Comment

Real Time Web Analytics