'getting substrings from string in Arduino
I am trying to get 3 substrings from a Serial String input using space as the delimiter. I tried converting the string input to a char array and using strtok but it didn't work. This was my code;
char buf[100];
char par1[10], par2[10], par3[10];
char del[] = " ";
String command;
void setup() {
Serial.begin(9600);
}
void loop() {
if (Serial.available() > 0) {
command = Serial.readStringUntil('\n');
command.toCharArray(buf, sizeof(buf));
par1 = strtok(buf, del);
buf = strtok(NULL, del);
par2 = strtok(buf, del);
buf = strtok(NULL, del);
par3 = strtok(buf, del);
printf("%s %s %s", par2, par2, par3);
}
}
How do I do this?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
