c++ 字符串切割
2017-04-06 10:11
323 查看
life is short, 唉!
demo1:
需求 “hello#world!” 按 ‘#’切割并输出, 注意这里我们知道是一个分隔符切成两段,比较简单。
demo2:
稍微复杂一点,比如一个网址 “http://blog.csdn.net/zjm750617105/article/details/62426843” 按 “/” 切割:
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/C++中字符串切割(split)
- c++的字符串char与string相互转化,以及string的,切割,替换字符,字符串拼接方法(object向c++转型系列教程2)
- c++的字符串char与string相互转化,以及string的,切割,替换字符,字符串拼接方法(object向c++转型系列教程2)
- C/C++切割字符串
- C++中string字符串切割的两种方法
- c++的字符串char与string相互转化,以及string的,切割,替换字符,字符串拼接方法
- C/C++--字符串切割及去两端空格
- C++字符串切割的两种方法
- C++字符串切割的两种方法
- c++的字符串char与string相互转化,以及string的,切割,替换字符,字符串相关的函数整理
- C++中用字符串来切割字符串
- Visual C++.NET中 字符串转换方法(转载)
- C++字符串完全指引之一 —— Win32 字符编码
- C++字符串完全指南(2) - 各种字符串类- CRT类
- 标准C++以及MFC6.0字符串的tokenize和split函数
- C++字符串完全指引之一 —— Win32 字符编码
- C++的字符串: C风格的字符串
- Visual C++.NET中的字符串转换方法(ZT)
- C++字符串完全指南(2) - 各种字符串类- CRT类
- Visual C++.NET中 字符串转换方法[1]