C分割字符串 sscanf vs strtok

sscanf

char buf[]="port=5000";
char key[100]="";
char value[100]="";
sscanf(buf,"%[^=]=%[^\n]",key,value);
printf("key=%s\n",key);
printf("value=%s\n",value);

    char *str0 = "abcd&efgh&ijklmnopq", str1[100]={0}, str2[100]={0}, str3[100]={0};
    sscanf(str0, "%[^&efgh]&efg%[^klm]%s", str1, str2, str3);
    printf("str1=%s, str2=%s, str3=%s\n", str1, str2, str3);
    return 0;

strtok

view source
print ?
char buf[]="port=5000";
char key[100]="";
char value[100]="";
strcpy(key,strtok(buf,"="));
strcpy(value,strtok(NULL,"="));
printf("key=%s\n",key);
printf("value=%s\n",value);


你可能感兴趣的:(C分割字符串 sscanf vs strtok)