您的位置:首页 > 编程语言 > C语言/C++

std::string字符串分割

2015-06-18 10:09 567 查看
废话不多说,直接上代码:
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###
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息