1. 程式人生 > >c split 函數 分割字符串 獲取指定index的子字符串

c split 函數 分割字符串 獲取指定index的子字符串

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的子字符串