I'm having trouble splitting a cstring "parameter=value" into two strings:
command
value
I don't imagine anyone has an idea? Here's what I have so far. This extracts value to "=value", which is close... But I can't figure out how to get "parameter".
void setup() {
Serial.begin(115200);
// the string to split
char* cmd = "parameter=value";
char * value = strchr(cmd, '=');
Serial.println( value );
// close! prints "=value", close!
}
void loop() {
delay(1000);
}
I have been, but for some reason every example of it's use that I can find simply prints each part of the string, as opposed to assigning them to variables. And I haven't had any luck assigning them to variables.
My string is always of the format "command=parameter".
Well after hours of working on it, I still can't get it. I see a String in my future.
Anyway, here's what I have so far, on the off chance this is easy for anyone:
void setup() {
Serial.begin(115200);
// the string to split
char cmd[] = "parameter=value";
char parameter[10] = "";
char value[10] = "";
char *ptr;
ptr = strtok (cmd,"=");
strcpy(parameter, ptr);
Serial.println( parameter );
// how to extract the "value" part of cmd, and put it into value?
}
void loop() {
delay(1000);
}
On a first call, the function expects a C string as argument for str, whose first character is used as the starting location to scan for tokens. In subsequent calls, the function expects a null pointer and uses the position right after the end of the last token as the new starting location for scanning.