这里写自定义目录标题
C++分割字符串std::string
将字符串str按照分割的字符串s,进行分割,将结果以std::vectorstd::string形式返回。
如str=“110.22,120.22,130.22,140.22”
s=","
std::vectorstd::stringret=[“110.22”,“120.22”,“130.22”,“140.22”];
代码如下
#include <iostream>
#include <vector>
#include <string>
std::vector<std::string> splitString(const std::string& str, const std::string& s)
{
std::vector<std::string> ret;
int sLen = s.size();
int index = 0;
int endIndex = str.find(s, index);
while(endIndex > 0)
{
if(endIndex > index)
{
std::string temp = str.substr(index, endIndex-index);
ret.push_back(temp);
index = endIndex + sLen;
}
endIndex = str.find(s, index);
}
if(index < str.size() )
{
std::string temp = str.substr(index, str.size() - index);
ret.push_back(temp);
}
return ret;
}