首页 > 代码库 > 字符拆分

字符拆分

#include <iostream>void split(char *psrc, const char separator, char **dest, int& num,bool bHoldSeparator=false){	num=0;	int index=0;	int i;	for(i=0;i<strlen(psrc);i++){		if(psrc[i]==separator)		{			num++;			index=0;			if(!bHoldSeparator) continue;		}		dest[num][index++]=psrc[i];	}}#define SEPERATE_LENGTH 128int main(){	char src[] = "Accsvr:tcp  -h    127.0.0.1    -p    20018";	char* dest[SEPERATE_LENGTH];	int i;	for(i=0;i<SEPERATE_LENGTH;i++){		dest[i]=(char*)malloc(SEPERATE_LENGTH*sizeof(char));		memset( dest[i],0,SEPERATE_LENGTH*sizeof(char) );	}	int num = 0;		split(src,‘-‘,dest,num);	for(i=0;i<SEPERATE_LENGTH;i++){		if( strlen(dest[i])>0 )			std::cout<<i<<" ---> "<<dest[i]<<std::endl;		delete [] dest[i];	}	std::cout<<"Over"<<std::endl;	getchar();	return 0;}

  

字符拆分