c++ 字符串切割

来源:互联网 发布:翻墙 mac 免费 2017 编辑:程序博客网 时间:2024/06/16 01:53

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” 按 “/” 切割:

有点事,晚上补
0 0
原创粉丝点击