C/C++实现split分割字符串
C++实现方法:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
void split(const string& src, const string& separator, vector<string>& dest)
{
string str = src;
string substring;
string::size_type start = 0, index;
do
{
index = str.find_first_of(separator,start);
if (index != string::npos)
{
substring = str.substr(start,index-start);
dest.push_back(substring);
start = str.find_first_not_of(separator,index);
if (start == string::npos) return;
}
}while(index != string::npos);
//the last token
substring = str.substr(start);
dest.push_back(substring);
}
int main()
{
string src = "Accsvr:tcp -h 127.0.0.1 -p \t 20018 ";
vector<string> d, s;
vector<string>::iterator p, q;
split(src,":",d);
for(p=d.begin();p!=d.end();++p)
{
cout << *p << endl;
s.clear();
split(*p," \t\n",s);
for (q=s.begin();q!=s.end();++q)
cout << "\t" << *q << endl;
}
return 0;
}
C语言实现方法一:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void split(char *src, const char *separator, char **dest, int *num)
{
char *pNext;
int count = 0;
if (src == NULL || strlen(src) == 0) return;
if (separator == NULL || strlen(separator) == 0) return;
pNext = strtok(src,separator);
while(pNext != NULL)
{
*dest++ = pNext;
++count;
pNext = strtok(NULL,separator);
}
*num = count;
}
int main()
{
char src[] = "Accsvr:tcp -h 127.0.0.1 -p\n 20018";
char *dest[128];
char *dest2[128];
int num = 0, num2 = 0;
int i, j;
split(src,":",dest,&num);
for (i=0;i<num;++i)
{
printf("|%s|\n",dest[i]);
split(dest[i]," \t\n",dest2,&num2);
for (j=0;j<num2;++j)
{
printf("|%s|\n",dest2[j]);
}
}
return 0;
}
C语言实现方法二:功能与方法一有区别
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void split(char *src, const char *separator, char **dest, int *num)
{
char *pSeparator, *pStart, *pEnd;
unsigned int sep_len;
int count = 0;
if (src == NULL || strlen(src) == 0) return;
pSeparator = (char *)malloc(16);
if (pSeparator == NULL) return;
if (separator == NULL || strlen(separator) == 0) strcpy(pSeparator," ");/* one blank by default */
else strcpy(pSeparator,separator);
sep_len = strlen(pSeparator);
pStart = src;
while(1)
{
pEnd = strstr(pStart, pSeparator);
if (pEnd != NULL)
{
memset(pEnd,'\0',sep_len);
*dest++ = pStart;
pEnd = pEnd + sep_len;
pStart = pEnd;
++count;
}
else
{
*dest = pStart;
++count;
break;
}
}
*num = count;
if (pSeparator != NULL) free(pSeparator);
}
int main()
{
char src[] = "Accsvr:tcp -h 127.0.0.1 -p 20018";
char *dest[128];
char *dest2[128];
int num = 0, num2 = 0;
int i, j;
split(src,":",dest,&num);
for (i=0;i<num;++i)
{
printf("|%s|\n",dest[i]);
split(dest[i],"\t",dest2,&num2);
for (j=0;j<num2;++j)
{
printf("|%s|\n",dest2[j]);
}
}
return 0;
}
1,分割字符串
char *strtok(char *str, const char *delim);
功能:分解字符串为一组字符串。str为要分解的字符串,delim为分隔符字符串。实质上的处理是,strtok在str中查找包含在delim中的字符并用NULL(’\0′)来替换,直到找遍整个字符串。
说明:首次调用时,str指向要分解的字符串,之后再次调用要把str设成NULL。strtok在str中查找包含在delim中的字符并用NULL(’\0′)来替换,直到找遍整个字符串。
返回值:从str开头开始的一个个被分割的串。当没有被分割的串时则返回NULL。所有delim中包含的字符都会被滤掉,并将被滤掉的地方设为一处分割的节点。
示例:
- #include <stdio.h>
- #include <string.h>
-
- int main(){
- //id,姓名,语文,数学,英语
- char str[]="2,张三,89,99,66";
- char *token=strtok(str,",");
- while(token!=NULL){
- printf("%s\t",token);
- token=strtok(NULL,",");
- }
- printf("\n");
- return 0;
- }
- 2 张三 89 99 66
2,分割字符串(还是分割字符串)
char *strsep(char **stringp, const char *delim);
- #include <stdio.h>
- #include <string.h>
-
- int main(){
- //id,姓名,语文,数学,英语
- char str[]="2,张三,89,99,66";
- //str是一个指针常量,而strsep的第一个参数需要一个指向指针的指针,所以不能对str做取地址操作,
- //这里再定义一个指针变量就可以取地址操作了。否则会出现段错误。
- char *strv=str;
- char *token=strsep( &strv,",");
- while(token!=NULL){
- printf("%s\t",token);
- token=strsep( &strv,",");
- }
- printf("\n");
- return 0;
- }