幸运的是,字符串包含实现这一点的成员函数,尽管有人可能会想出一个“更酷”的解决方案,但这是可行的(并且打算这样做):
#include <iostream>
#include <string>
#include <sstream>
int main(){
std::ostringstream oss;
oss << "GET /analysis HTTP/1.1\n\n"
<< "Host: localhost:4433\n\n"
<< "User-Agent: curl/7.47.0\n\n"
<< "Accept: */*\n\n"
<< "Authorization: Basic MTIzYWxpY2U6bWVyY3VyeQ==\n\n"
<< "Content-Length: 40\n\n"
<< "Content-Type: application/x-www-form-urlencoded\n\n"
<< "{\"u_id\": 62, \"g_id\": 14, \"a_type\": \"LR\"}";
std::string content = oss.str();
std::string delimiterStart = "Basic ";
std::string delimiterEnd = " ";
int start = content.find(delimiterStart) + delimiterStart.length();
std::string partial = content.substr(start, content.length());
partial = partial.substr(0, partial.find(delimiterEnd));
std::cout<<"STR: "<< partial;
return 0;
}
这是假设您知道两个定界符,不管是什么情况,您都需要它,否则,如果您不知道从何处到何处“获取”任何内容,您将如何提取任何内容?