标签:c split
char* get_sub_split(char* path,const char* delim,int index)
{
//static const char *delim = ".";
int i = 0;
char* p = strtok(path, delim);
//printf("i=%d,%s\\n", i,p);
if(i==index)
{
return p;
}
i++;
while((p = strtok(NULL, delim))){
//printf("i=%d,%s\\n", i,p);
if(i==index)
{
return p;
}
i++;
}
return NULL;
}
调用示例:
char srcstr[1024]={0};
strcpy(srcstr,"messages.global.Input1=true");
char *p = get_sub_split(srcstr,".",2);
printf("get %dst conten:%s\\n",2,p );
strcpy(srcstr,p);
char srcstr2[1024]={0};
strcpy(srcstr2,p);//由于分割函数会改变源字符串,所以不能使用同一源给多次调用赋值
printf("get %dst conten:%s\\n",0, get_sub_split(srcstr,"=",0));
printf("get %dst conten:%s\\n",1, get_sub_split(srcstr2,"=",1));
本文出自 “软件技术” 博客,请务必保留此出处http://danielllf.blog.51cto.com/2442311/1947133
c split 函数 分割字符串 获取指定index的子字符串
标签:c split
原文地址:http://danielllf.blog.51cto.com/2442311/1947133