[英]get substring from C string in C++
我有C弦
char s[] = "n1=1&n2=2&name=test&sername=test2";
我需要從字符串值名稱(即“ test”)中提取一個單獨的變量。
因此,我需要找到“&name =“與下一個&
因為您將其標記為C ++,所以我將使用std::string
代替:
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("&"));
您也可以使用正則表達式執行此操作,並一次捕獲所有這些值,還可以節省創建字符串的時間。
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;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.