strtok--常用C标准库函数

一:函数原型

#include <string.h>
char *strtok(char *str, const char *delim);

二:功能阐述

 1.该函数用传过来的标志delim参数,来分割字符串str。
 2.把str中出现的delim的位置,填充'\0',以达到分割字符串str的目的。
 3.第一次传入的str必须为str,若打算第二次还继续分割str,则可以传入NULL,但是delim必须传。
 4.返回值为指向被分割的字符串的指针。解析完返回空。
 例如:若分割的字符串为"hello,linux,xiaoniu";分割副传入",",则第一次调用返回的指针指向h,
 第二次指向linux的l.详细看第三部分的例子。

三:用法举例

#include <string.h>
#include <stdio.h>

int main(int argc, char *argv[])
{
    char strBuff[100] = "hello,linux,xiaoniu!";
    char *b = NULL;

    b = strtok(strBuff, ",");
    printf("%s\n", b);
    b = strtok(NULL, ",");
    printf("%s\n", b);
    b = strtok(NULL, ",");
    printf("%s\n", b);
    b = strtok(NULL, ",");
    if(NULL == b){      {
        printf("now b is NULL!\n");
    } else {
        printf("%s\n", b);
    }

     return 0;
}

编译并运行程序输出结果:

hello
linux
xiaoniu!
now b is NULL!

猜你喜欢

转载自blog.csdn.net/qq_37468954/article/details/81777733