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

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++ 分割字符串