C语言中经典的拆分字符串一般用strtok, 相信多数人一直用它。下面样例是用sscanf拆分字符串,以供大家参考:
char *param = "FontName=Arial,FontSize=32,PrimaryColour=0x00FFFFFF,MarginL=10,Underline=1,color=&H00000000";
while(sscanf(param, "%[^=]=%[^,],%s", skey, sval, snext))
{
printf("key = %s, val=%s, next=%s\n", skey, sval, snext);
if (strncmp(skey, snext, strlen(skey)) )
{
param = snext;
}
else
break;
}
下面样例是从C++ Libaray Reference中copy,以作比较,体会它们的差异。
若对sscanf的用法熟悉,可对不同的应用作许多相应的变化。
/* strtok example */
#include
#include
int main ()
{
char str[] ="- This, a sample string.";
char * pch;
printf ("Splitting string \"%s\" into tokens:\n",str);
pch = strtok (str," ,.-");
while (pch != NULL)
{
printf ("%s\n",pch);
pch = strtok (NULL, " ,.-");
}
return 0;
}
本程序没有经过大量测试应用过,仅作参考用,有问题可联系:
阅读(2774) | 评论(0) | 转发(0) |