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

c++ 字符串切割

2017-04-06 10:11 323 查看
life is short, 唉!

demo1:

需求 “hello#world!” 按 ‘#’切割并输出, 注意这里我们知道是一个分隔符切成两段,比较简单。

#include <string>
#include <vector>
std::vector<std::string> split(std::string str,std::string pattern)
{
std::string::size_type pos;
std::vector<std::string> result;
pos=str.find(pattern,0);
std::string s=str.substr(0,pos);
result.push_back(s);
std::string s2 = str.substr(pos+1,str.size());
result.push_back(s2);
return result;
}
int main(){

std::string s = "hello#world!";
std::vector<std::string> results = split(s, "#");
for(int i=0; i<results.size();i++){
std::cout << results[i] << std::endl;
}
}


demo2:

稍微复杂一点,比如一个网址 “http://blog.csdn.net/zjm750617105/article/details/62426843” 按 “/” 切割:

有点事,晚上补
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c++ string split