自己写了一个字符串分割函数,可以根据提供的分隔符串列表将指定的字符串分割为若干个字符串,分隔符可以是单个字符也可以是字符串,可以设定是否压缩分隔符串(即当两个或者两个以上分隔符串连续出现时不生成空串),也可以设定是否把查找到的分隔符串也插入到结果中。需要注意的是结果是动态分配的内存,使用完毕后需要自己释放。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/**
* Split a string into some strings according to a list of separators.
*
* @Param dest out: storage the strings has be split.
* @Param count out: the number of strings has be split successfully, 0 for failed to split.
* @Param s_str in: the strings for split.
* @Param separator in: the list of split separators.
* @Param number_separator in: the numbers of separators.
* @Param compress_separator in: will be create a empty string when two split adjacent
* if compress_separator > 0 and not for compress_separator == 0
* @Param keep_separator in: the separators will be put into parameter 'dest' if keep_separator > 0
*/
int strsplit(char **