Splitting cstring parameter=value into components... (solved)

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);
}

Take a look at the strtok() function

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".

Doing this causes a crash:

char * pch = strtok (cmd,"=");
Serial.println(pch);

Update:

Aha, this works:

char str[] = "parameter=value";

char * command;
command = strtok (str,"=");  
Serial.println(command);

But how to get the second part of it ("value") and assign it to a variable?

strtok() finds the 'next' instance of the character that you're looking for. Run it a second time and it will give you the value.

Instead of printing, you can copy

char parameter[10] = "";
char value[10] = "";



char *ptr;
// get the parameter
ptr = strtok (str,"=");
// check if found
if(parameter != NULL)
{
  // copy
  strcpy(parameter, ptr);

  // get the value
  ptr = strtok (str,"=");
  // check if found
  if(value !=NULL)
  {
      // copy
      strcpy(parameter, ptr);

  }
}
Serial.print("Parameter = ");
Serial.println(parameter);
Serial.print("Value = ");
Serial.println(value);

If you understand an example, use it.
If you don't understand an example, don't use it.

In the spirit of you footer, I'm trying to avoid using that loop, which is what I see in every example of using this function.

Isn't there a simpler method given that I know exactly how many of the delimiters are in my string?

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

  // the string to split
  //char* cmd = "parameter=value";
  char cmd[] = "parameter=value";


  char parameter[10] = "";
  char value[10] = "";

  char *ptr;
  
  ptr = strtok (cmd,"="); 
  strcpy(parameter, ptr);

  ptr = strtok (cmd,"="); 
  strcpy(value, ptr);

  Serial.println(parameter);
  Serial.println(value);
  
}

void loop() { 
  delay(1000);
}

This just prints:

parameter
parameter

It's enough to make me consider crossing over to the dark side of the String...

There is no loop in the code that I presented.

  char * value =  strchr(cmd, '=');

Serial.println( value );

If you only want the value, increment value first.

// find the value part in cmd
  char * value =  strchr(cmd, '=');
  value++;
  Serial.println( value );

value++ increments the pointer to point to the next character. You will need to add a check to see if the '=' actually is found; see previous code.

I'm not quite sure what you're trying to achieve with the part that follows that code. Do you have multiple parameter/value pairs in cmd?

A good YT video on strtok()

The parse example in Serial Input Basics illustrates the use of strtok()

...R

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);
}

So blissfully easy using the dreaded String class:

  String cmd2="parameter=value";
  int idx = cmd2.indexOf('='); 
  String parameter2 = cmd2.substring(0, idx);
  String value2 = cmd2.substring(idx+1, cmd2.length());
  Serial.println( parameter2 );
  Serial.println( value2 );
   char cmd[] = "para=value";
    char pa[10];
    char val[10];

    char *token = strtok(cmd, "=");
    strcpy(pa, token);

    token = strtok(NULL, "=");
    strcpy(val, token);

wrybread:
// how to extract the "value" part of cmd, and put it into value?

That's covered here:

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.

... and in @Robin2's tutorial.

EDIT:
... and in @arduino_new's post just before I hit "Save".

wrybread:
So blissfully easy using the dreaded String class:

And, potentially, blissfully problematic.

Thank you arduino_new!!

Works perfectly, phew.

wrybread:
Thank you arduino_new!!

Works perfectly, phew.

Reply #10 does the same as reply #3 :wink: The latter however added hardening.