use strtok() to extract keys and values stored in key/value pairs within a string : string token « string « C++ Tutorial






#include <iostream>
#include <cstring>

using namespace std;
int main() {
  char kvpairs[] = "count=10, name=\"Joe, jr.\", max=100, min=0.01";
  char kvdelims[] = " =,";
  char *tok;
  tok = strtok(kvpairs, kvdelims);
  
  while(tok) {
    cout << "Key: " << tok << " ";
    if(!strcmp("name", tok)) {
      tok = strtok(NULL, "\"");
    }
    else {
      tok = strtok(NULL, kvdelims);
    }
    cout << "Value: " << tok << endl;

    tok = strtok(NULL, kvdelims);
   }

  return 0;
}








15.25.string token
15.25.1.use strtok() to tokenize a sentence
15.25.2.use strtok() to extract keys and values stored in key/value pairs within a string