Recently need to use C language to implement a split string function, the separator is "=", and then get = Both sides of the data
After using Java habits to write C language code always want to call the function, but there is no split function in C, for the beginning of the string segmentation is the most stupid way is based on the equal sign in the string position and then use the strncpy function interception, but this interception has a great disadvantage, when the need to intercept the string more, The position of the equal sign is not fixed, so the intercept string does not achieve the desired effect, and later found a very useful function in C sscanf. See the code below.
#include <stdio.h>int main () {printf ("hello\n"), char *test = "aaa=sss"; char Key[4];char value[40];sscanf (test, "% [^=]=%s ", Key,value);p rintf ("%s\n ", key);p rintf ("%s\n ", value); return 0;}
This can be obtained = The data on both sides is stored in key and value, respectively.
C-Language split string