Separar caracteres en Arduino

Programa en Arduino para separar caracteres…

 


String split = "133,215,365";

void setup() {
  Serial.begin(9600);
}

void loop() {
int parte1 = getValue(split,',',0).toInt();
Serial.print(parte1);
delay(1000);
}

String getValue(String data, char separator, int index)
{
  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]) : "";
}

2 comentarios en «Separar caracteres en Arduino»

  1. Excelente aporte, por fin un código que realmente funciona y que de verdad separa lo que uno necesita, sin restricciones, felicitaciones

    Responder

Responder a admin Cancelar la respuesta