如"A|B|C|D" 根据|分割,得到数组["A","B","C","D"]
char *propertyArr[4] = { "-1","-1" ,"-1","-1" }; custom_string("A|B|C|D", "|", propertyArr, 4);
void custom_string(char *str, char *delimiter, char *arr[], int maxLength) { char *token = strtok(str, delimiter); int arrIndex = 0; while (token != NULL && arrIndex < maxLength) { TC_write_syslog("token : %s\n", token); arr[arrIndex++] = token; // 存储分割后的子串 TC_write_syslog("test :%d, %s\n", arrIndex, token); token = strtok(NULL, delimiter); // 获取下一个子串 } }
这里要注意数组长度的问题,需要事先给定数组的长度。
如果你确定数组的长度,则尽量将长度给得大一些。
标签:分割,delimiter,C语言,char,token,数组,字符串,arrIndex From: https://www.cnblogs.com/wwssgg/p/18561062