c/c++分割字符串
版权声明:
本文为博主原创文章,转载请声明原文链接...谢谢。o_0。
更新时间:
2018-11-13 21:07:12
温馨提示:
学无止境,技术类文章有它的时效性,请留意文章更新时间,如发现内容有误请留言指出,防止别人"踩坑",我会及时更新文章
#include <string> #include <vector> //定义tstring类型 #ifndef __TSTRING__ #define __TSTRING__ # ifdef _UNICODE typedef std::wstring tstring; # else typedef std::string tstring; # endif #endif void SplitString(const tstring& s, std::vector<tstring>& v, const tstring& c) { tstring::size_type pos1, pos2; pos2 = s.find(c); pos1 = 0; while (tstring::npos != pos2) { v.push_back(s.substr(pos1, pos2 - pos1)); pos1 = pos2 + c.size(); pos2 = s.find(c, pos1); } if (pos1 != s.length()) v.push_back(s.substr(pos1)); }
使用方法
TCHAR headers[] = _T( "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36\r\n" "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8\r\n" "Accept-Language: zh-cn\r\n" "Cookie: test1=11111111111\r\n" "Connection: Keep - Alive"); //接口分隔好的字符串 std::vector<tstring> arr; SplitString(headers, arr, _T("\r"));