C语言的字符串按照指定字符串分割操作

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qiantanlong/article/details/78505306

函数原型:char * strtok (char *str, const char * delimiters);
参数:str,待分割的字符串(c-string);delimiters,分割符字符串。
该函数用来将字符串分割成一个个片段。参数str指向欲分割的字符串,参数delimiters则为分割字符串中包含的所有字符。当strtok()在参数s的字符串中发现参数delimiters中包涵的分割字符时,则会将该字符改为\0 字符。在第一次调用时,strtok()必需给予参数s字符串,往后的调用则将参数s设置成NULL。每次调用成功则返回指向被分割出片段的指针。
需要注意的是,使用该函数进行字符串分割时,会破坏被分解字符串的完整,调用前和调用后的s已经不一样了。第一次分割之后,原字符串str是分割完成之后的第一个字符串,剩余的字符串存储在一个静态变量中,因此多线程同时访问该静态变量时,则会出现错误。
例子:

#include <string.h>

#include <stdio.h>
#pragma warning(disable:4996)
#include <stdlib.h>  


int main()
{
	char str[] = "我,是,中国,程序员";
	char *ptr;
	char *p;
	printf("开始前:  str=%s\n", str);
	printf("开始分割:\n");
	ptr = strtok(str, ",");
	while (ptr != NULL) {
		printf("ptr=%s\n", ptr);
		ptr = strtok(NULL, ",");
	}
	getchar();
}


猜你喜欢

转载自blog.csdn.net/qiantanlong/article/details/78505306