c代码实现字符串分割 (通用于字节分割)
在公司使用过的
输入
nod : 开始分割的起始位置
source_str:代分割的字符串地址
dest_str : 保存分割的数组地址
dest_str_len:保存分割的数组长度
ch:分割的字符
return:分割以后新的起始位置
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
static int split_str(int nod, uint8_t *source_str, uint8_t *dest_str, int dest_str_len, uint8_t ch)
{
if (NULL == source_str || NULL == dest_str)
{
return -1;
}
int len;
len = strlen(source_str);
int i;
for (i = nod; i < len; i++)
{
if (*(source_str + i) == ch)
{
break;
}
}
memcpy(dest_str, source_str + nod, i - nod);
if ((i - nod) < dest_str_len)
{
dest_str[i - nod] = '\0';
}
if (i == len)
return 0;
else
return (++i);
}
int main()
{
char str_s[100] = "$GNGGA,, 024817.00,3109.95680723,N,12117.28029833,E,1,28,0.6,44.9440,M,0.0000,M,,*7D,,,";
int nod = 0;
char str_d[100] = {0};
int i;
for (i = 0; i < 20; i++)
{
nod = split_str(nod, str_s, str_d, sizeof(str_d), ',');
printf("%s\n", str_d);
}
}