前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >C语言 字符串分割

C语言 字符串分割

作者头像
全栈程序员站长
发布2022-06-27 21:14:55
发布2022-06-27 21:14:55
6.9K00
代码可运行
举报
运行总次数:0
代码可运行

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

C语言 字符串分割

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

二、例子代码

代码语言:javascript
代码运行次数:0
运行
复制
#include <stdio.h>
#include <string.h>
 
/*
 * 函数: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<strLen-1)
	{
		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 <string.h>

原型

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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年6月9,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • C语言 字符串分割
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档