废话不多说,直接上代码:
std::vector<std::string> split(std::string str, std::string pat)
{
std::vector<std::string> bufStr;
while (true)
{
int index = str.find(pat);
std::string subStr = str.substr(0, index);
if (!subStr.empty())
bufStr.push_back(subStr);
str.erase(0, index + pat.size());
if (index == -1)
break;
}
return bufStr;
}
使用方式:
std::vector<std::string> plits = split("192.168.0.1", ".");
for (int i = 0; i < plits.size(); i++)
{
printf("###%s###", plits.at(i).c_str());
}
最后输出为:
###192###
###168###
###0###
###1###