C++ 分割字符串
2014-04-11 20:01
351 查看
一. 说明
例如有这样的一组字符串(双引号引住的字符串)Item1 : "123456 abcdef 12456 888"
Item2 : "1234568 fggdgd 121122456 666"
两组字符串的特点是, 每一组字符串中有4段字符, 每段字符之间是空格分开.
二. 使用C中的strtok函数进行字符串分割
char *strtok(char *str, const char *delim);其中:str为要分解的字符串,delim为分隔符字符串。
返回值:从str开头开始的一个个被分割的串。当没有被分割的串时则返回NULL。
2.1 简单使用
#include <string.h> #include <stdio.h> int main() { char pItem[] = "123456 abcdef 12456 888"; const char *d = " "; // 这里的分割符是空格 char *p = 0; p = strtok(s, d); while(p) { printf("%s\n", p); p = strtok(NULL, d); } return 0; } 输出: 123456 abcdef 12456 888
2.2 分割付可以使多个
#include <string.h> #include <stdio.h> int main() { char pItem[] = "123456 abcdef*12456 888"; const char *d = " *"; // 这里的分割符是空格或者是星号 char *p = 0; p = strtok(s, d); while(p) { printf("%s\n", p); p = strtok(NULL, d); } return 0; } 输出: 123456 abcdef 12456 888
2.3 注意strtok的第一个参数
#include <string.h> #include <stdio.h> int main() { // 如果是这样会报错, 这样子代表它是一个常量 char* pItem = "123456 abcdef*12456 888"; const char *d = " *"; // 这里的分割符是空格或者是星号 char *p = 0; p = strtok(s, d); while(p) { printf("%s\n", p); p = strtok(NULL, d); } return 0; }
2.3 看这个
#include <string.h> #include <stdio.h> int main() { std::string strItem = "123456 abcdef*12456 888"; const char *d = " *"; // 这里的分割符是空格或者是星号 char *p = 0; p = strtok((char*)strItem.c_str(), d); // 这里要强制转换 while(p) { printf("%s\n", p); p = strtok(NULL, d); } return 0; } 分割前strItem在内存中是: "123456 abcdef*12456 888\0" 分割后是: "123456\0 abcdef\012456\0 888\0" (没一段后面被加了结束符号)
三. 使用std::string的分割方法
3.1 简单实现
int main() { std::string strData = "123456 abcdef 12456 888"; std::string strTemp; std::string::size_type nBegin = 0, nEnd = 0; nBegin = nEnd; nBegin = strData.find_first_not_of(" ", nBegin); nEnd = strData.find_first_of(" ", nBegin); strTemp = strData.substr(nBegin, nEnd - nBegin); std::cout << strTemp << std::endl; nBegin = nEnd; nBegin = strData.find_first_not_of(" ", nBegin); nEnd = strData.find_first_of(" ", nBegin); strTemp = strData.substr(nBegin, nEnd - nBegin); std::cout << strTemp << std::endl; nBegin = nEnd; nBegin = strData.find_first_not_of(" ", nBegin); nEnd = strData.find_first_of(" ", nBegin); strTemp = strData.substr(nBegin, nEnd - nBegin); std::cout << strTemp << std::endl; nBegin = nEnd; nBegin = strData.find_first_not_of(" ", nBegin); nEnd = strData.find_first_of(" ", nBegin); strTemp = strData.substr(nBegin, nEnd - nBegin); std::cout << strTemp << std::endl; return 0; }
3.2 改进一下
使不使用循环, 看你自己的需求.int main() { std::string strData = "123456 abcdef 12456 888"; std::string strTemp; std::string::size_type nBegin = 0, nEnd = 0; while(std::string::npos != (nBegin = strData.find_first_not_of(" ", nEnd))) { nEnd = strData.find_first_of(" ", nBegin); strTemp = strData.substr(nBegin, nEnd - nBegin); std::cout << strTemp << std::endl; } return 0; }
还有一种情况: 就是例如: "123456,,abcdef,,12456,,888"
如果使用上面函数会提取出4串字符串, 但是有时在两个"逗号"之间也是一个数据, 只是这个数据时空数据.
上面的方法会把连续的"逗号"作为一个分割符.
这里的函数是, 一个"逗号"作为一个分割符, 所以"123456,,abcdef,,12456,,,,888"这段数据会分割出9串字符串, 其中有5串是空字符串.
(这里有8个逗号, 刚好分割出9段字符串)
int main()
{
std::string strData = "123456,,,,abcdef,,,12456,,,,888";
std::string strTemp;
std::string::size_type nBegin = 0, nEnd = 0;
while(std::string::npos != (nEnd = strData.find_first_of(",", nBegin)))
{
strTemp = strData.substr(nBegin, nEnd - nBegin);
stData.m_strVectorData.push_back(new std::string(strTemp));
nBegin = nEnd + 1;
}
strTemp = strData.substr(nBegin, strData.length() - nBegin);
stData.m_strVectorData.push_back(new std::string(strTemp));
return 0;
}
四. 小结
在这些代码中 strtok比较灵活, 它可以同时使用多个分割符, 也比较有效率. 使用std::string明显是有一点慢的.相关文章推荐
- C++分割字符串-win7-vs2012
- C++&nbsp;字符串分割
- [C/C++标准库]_[初级]_[分割字符串Split]
- c++分割字符串(类似于boost::split)
- C/C++_字符串分割strtok函数
- C++常用字符串分割方法实例汇总
- C/C++中的Split函数(字符串自动分割)
- C++中String类的字符串分割实现
- C++中字符串的分割
- c 以及 c++中字符串分割
- C++ 读取文件内容,分割出字符串
- c++中的字符串分割
- C++分割字符串
- C++ 字符串分割
- c++编写字符串分割函数
- C++ 字符串分割
- 牛客网-华为在线编程004--C++字符串的分割
- C/C++字符串分割
- (C++)字符串分割
- c++字符串分割 和 c++读取文件内容