c++ 字符串分割写法 一般比较常用 懒得每次都写 记录下
作者:互联网
std::vector<std::string> split(const std::string& str, const std::string& separator)
{
std::vector<std::string> str_result;
if (str.empty() || separator.empty()){return str_result;}
std::string::size_type pos = str.find_first_of(separator);
if (pos != std::string::npos)
{
str_result.emplace_back(str.substr(0, pos));
while (pos != std::string::npos)
{
std::string::size_type next = str.find(separator, pos + separator.size());
std::string str_temp_sub;
if (next != std::string::npos)
{
str_temp_sub = str.substr(pos + separator.size(), next - (pos + separator.size()));
str_result.emplace_back(str_temp_sub);
}
else
{
str_temp_sub = str.substr(pos + separator.size(), -1);
str_result.emplace_back(str_temp_sub);
}
pos = next;
}
}
return str_result;
}
int main()
{
for (auto& v : split("285894_A789646_65489", "_"))
std::cout << v << std::endl;
}
标签:std,懒得,string,pos,c++,separator,result,str,写法 来源: https://www.cnblogs.com/guolongzheng/p/14119808.html