I have C string
char s[] = "n1=1&n2=2&name=test&sername=test2";
I need to take from the string value name, ie, "test" and written in a separate variable.
So I need to find the value between "&name=" and the next &
Because you tagged this as C++ I'll use std::string instead:
char s[] = "n1=1&n2=2&name=test&sername=test2";
string str(s);
string slice = str.substr(str.find("name=") + 5);
string name = slice.substr(0, slice.find("&"));
You could also do this with regex and capture all those values at once, also saving the time of creating a string.
char s[] = "n1=1&n2=2&name=test&sername=test2";
std::regex e ("n1=(.*)&n2=(.*)&name=(.*)&sername=(.*)");
std::cmatch cm;
std::regex_match(s,cm,e);
cout << cm[3] << endl;
std::stringand use substrstrlen()to find an end iterator,std::search()to find the position of the where the parameter starts, andstd::find()where it ends. What part exactly is your problem?sshould beconsthere.&to get the collection ofpairs(a=b), then split on=to get a key and value for each pair.