我有C字符串得到C字符串子串在C++
char s[] = "n1=1&n2=2&name=test&sername=test2";
我需要從字符串值的名稱,即「測試」,並寫在一個獨立的變量來取。
所以我需要找到之間的值「&名稱=」和下一個&
我有C字符串得到C字符串子串在C++
char s[] = "n1=1&n2=2&name=test&sername=test2";
我需要從字符串值的名稱,即「測試」,並寫在一個獨立的變量來取。
所以我需要找到之間的值「&名稱=」和下一個&
因爲你標記這是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;
Thanks.It是我需要的。 –
@NastyaGorobets偉大:-)然後考慮upvoting和標記答案被接受。 – VillasV
你可以建立一個'的std :: string'並使用[SUBSTR(http://www.cplusplus.com/reference/string/string/substr/) –
所以,你會使用'strlen的( )'查找結束迭代器,'std :: search()'查找參數開始位置,以及'std :: find()'結束位置。你的問題究竟是什麼部分? –
您可以將指針用作數組。 – Polak