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));
时间: 2024-09-29 09:29:27