C语言 字符串分割

2026-07-25 09:31:39

大家好,又见面了,我是你们的朋友全栈君。

C语言 字符串分割一、简述

记–字符串分割,strtok()函数的使用例子、自己简单实现split()函数。

二、例子代码

代码语言:javascript复制#include

#include

/*

* 函数:split

* 描述:按指定分隔符分割字符串

* 参数:

* str:要分割的字符串

* strLen:要分割的字符串的长度

* splitChar:分隔符

* index:获取第几部分, 1<=index

* result:结果字符串, result = str[index-1];

* maxLen:指定结果的最大长度

* 返回值:

* >=0:成功, 结果长度

* 其它:失败

* 例子:

* split("11;22;33", 8, ';', 2, result. 16);

* 结果result是:22.

*/

int split(const char* str, int strLen, const char* splitChar, int index, char* result, int maxLen)

{

int i = 0;

int ret = 0;

int findLen = 0;

int findFlag = 0;

int startIndex = 0;

int splitCharLen = 0;

//合法性判断

if(NULL == str || NULL == result || NULL == splitChar || index<=0)

{

return -1;

}

splitCharLen = strlen(splitChar);

findLen = strLen-splitCharLen;

if(findLen<0)

{

return -2;

}

//查找结果的左右分隔符位置

for(; i<=findLen && str[i] != '\0'; i++)

{

if(strncmp(&str[i], splitChar, splitCharLen) == 0)

{

if(0 == findFlag)//find the left

{

startIndex++;

if(1 == index)//第一个直接返回

{

strncpy(result, &str[0], i);

ret = i;

break;

}

else if(startIndex+1 == index)

{

startIndex = i;

findFlag = 1;

}

}

else//find the right

{

findFlag = 2;

break;

}

}

}

//截取结果

if(0 != findFlag && startIndex

{

startIndex += splitCharLen;

ret = i-startIndex;//结果的字符个数

if(ret>maxLen || ret>strLen)

{

ret = 0;

}

else if(ret>0)

{

strncpy(result, &str[startIndex], ret);

ret = strlen(result);

}

}

return ret;

}

int main(void)

{

{

const char* splitChar = ";";

printf("\n==========strtok1==========\n");

char str1[128] = "Keep;learning;and;study;hard";

printf("str1:\"%s\", splitChar:\"%s\"\n", str1, splitChar);

char* ptr = strtok(str1, splitChar);

for(; ptr != NULL; )

{

printf("%s\n", ptr);

ptr = strtok(NULL, splitChar);

}

printf("strtok after, str1:%s\n", str1);

printf("\n==========split1==========\n");

char str2[128] = "Keep;learning;and;study;hard";

printf("str2:\"%s\", splitChar:\"%s\"\n", str2, splitChar);

int i;

int ret = 1;

char result[128];

int strLen = strlen(str2);

int resultLen = sizeof(result);

for(i=1; ret>0; i++)

{

memset(result, 0, sizeof(result));

ret = split(str2, strLen, splitChar, i, result, resultLen);

if(ret>0)

{

printf("%s\n", result);

}

}

printf("split after, str2:%s\n", str2);

}

{

const char* splitChar = "##";

printf("\n==========strtok2==========\n");

char str1[128] = "Keep##learning##and##study##hard";

printf("str1:\"%s\", splitChar:\"%s\"\n", str1, splitChar);

char* ptr = strtok(str1, splitChar);

for(; ptr != NULL; )

{

printf("%s\n", ptr);

ptr = strtok(NULL, splitChar);

}

printf("strtok after, str1:%s\n", str1);

printf("\n==========split2==========\n");

char str2[128] = "Keep##learning##and##study##hard";

printf("str2:\"%s\", splitChar:\"%s\"\n", str2, splitChar);

int i;

int ret = 1;

char result[128];

int strLen = strlen(str2);

int resultLen = sizeof(result);

for(i=1; ret>0; i++)

{

memset(result, 0, sizeof(result));

ret = split(str2, strLen, splitChar, i, result, resultLen);

if(ret>0)

{

printf("%s\n", result);

}

}

printf("split after, str2:%s\n", str2);

}

return 0;

}三、测试结果

四、总结

strtok()函数介绍

功能

分割字符串

头文件

#include

原型

char *strtok(char *str, const char *delim);

参数

str:要分割的字符串

delim:分隔符

返回值

成功:非空指针,分割后的结果字符串

失败:NULL,分割后没有得到有效的字符串

备注

第一次调用strtok()时,要解析的字符串应在str中指定。 在每个随后的应解析相同字符串的调用中,str必须置空。

即第一次是strtok(str, spplitChar);

后面调用strtok(NULL, spplitChar);

注意:调用strtok之后会修改原来的str

详情请查询man手册,man strtok

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/133071.html原文链接:https://javaforall.cn

Copyright © 2022 世界杯奖杯_男足世界杯预选赛 - cbatop.com All Rights Reserved.