Categories
arduino code

arduino split string to array

Arduino แบ่งข้อความจาก คอมม่า (,)

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

void loop () { 
  String text = "go,is,go,4,maker";
  char sz[128];
  text.toCharArray(sz, sizeof(sz));
  String words[12];
  char *str, *p = sz;
  int i = 0;
  while (str = strtok (p, ",")){
    words[i] = str;
    p = NULL;
    ++i;
  }
  // print array 
  for(int i=0;i<sizeof(words)/sizeof(words[0]);i++) {
    if (words[i].length() > 0) {
      Serial.println(words[i]);
    }
  }
}

Comments are closed.