English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C 语言基础教程

C 语言流程控制

Fonctions en C

Tableaux en C

Pointeurs en C

Chaines de caractères en C

C 语言结构体

C 语言文件

C 其他

C 语言参考手册

C 库函数 strtok() 使用方法及示例

Bibliothèque standard en C - <string.h>

C 库函数 char *strtok(char *str, const char *delim) 分解字符串 str 为一组字符串,delim 为分隔符。

声明

下面是 strtok() 函数的声明。

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

参数

  • str -- 要被分解成一组小字符串的字符串。
  • delim -- 包含分隔符的 C 字符串。

返回值

该函数返回被分解的第一个子字符串,如果没有可检索的字符串,则返回一个空指针。

在线示例

下面的示例演示了 strtok() 函数的用法。

#include <string.h>
#include <stdio.h>
int main () {
   char str[80] = "This is - fr.oldtoolbag.com - website";
   const char s[2] = "-";
   char *token;
   /* 获取第一个子字符串 */
   token = strtok(str, s);
   /* 继续获取其他的子字符串 */
   while( token != NULL ) {
      printf( "%s\n", token );
      token = strtok(NULL, s);
   }
   return(0);
}

Compilons et exécutons le programme ci-dessus, cela produira le résultat suivant :

Ceci est 
 fr.oldtoolbag.com 
 site web

Bibliothèque standard en C - <string.h>