C++字符串分割
2016-08-10 10:50
211 查看
字符串分割经常用到,这里做一个记录。方便查阅。
1.使用strtok();其中
采用strtok(),分隔符可以是多种,如 * ,#中的一种或几种的组合
2.使用string.substr();其中
采用string.substr(),分隔符只能是一种,如 * ,#中的一种
1.使用strtok();其中
采用strtok(),分隔符可以是多种,如 * ,#中的一种或几种的组合
vector<string> stringSplit(string s, const char * split) { vector<string> result; const int sLen = s.length(); char *cs = new char[sLen + 1]; strcpy(cs, s.data()); char *p; p = strtok(cs, split); while (p) { printf("%s\n", p); string tmp(p); result.push_back(tmp); p = strtok(NULL, split); } return result; }
2.使用string.substr();其中
采用string.substr(),分隔符只能是一种,如 * ,#中的一种
vector<string> vec; int j = 0; for (int i = 0; i<str.size(); i++){ if (str[i] == ' '){ string tmp = str.substr(j, i - j); vec.push_back(tmp); j = i + 1; } if (i == str.size() - 1){ string tmp = str.substr(j, i - j + 1); vec.push_back(tmp); } }
相关文章推荐
- C++中使用指定字符分割字符串
- C++分割字符串的类
- 【转】C/C++中的Split函数(字符串自动分割)
- c++中怎么分割字符串之strtok 函数
- 分割字符串 c++
- 标准C++ 与MFC 6.0 中字符串的分割
- C/C++中的输出字符串分割和对齐
- C++字符串分割
- c/c++ 字符串分割 (strtok)
- C/C++_字符串分割strtok函数
- C/C++分割字符串:strtok函数
- C++MFC分割字符串
- C++ 分割字符串
- c++字符串分割(转)
- C/C++中的字符串分割
- C++中利用STD库函数分割字符串
- c++编写字符串分割函数
- c++ 字符串分割
- C++ 使用正则表达式分割字符串
- [C++]逐行读取文件与分割字符串方法