当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C语言 strtok()、strtok_r()用法及代码示例


C提供了两个函数strtok()和strtok_r(),用于通过某些定界符分割字符串。拆分字符串是非常常见的任务。例如,我们用逗号分隔文件中的项目列表,并且希望将单个项目放在数组中。

strtok()

// Splits str[] according to given delimiters.
// and returns next token. It needs to be called
// in a loop to get all tokens. It returns NULL
// when there are no more tokens.
char * strtok(char str[], const char *delims);
// A C/C++ program for splitting a string 
// using strtok() 
#include <stdio.h> 
#include <string.h> 
  
int main() 
{ 
    char str[] = "Geeks-for-Geeks"; 
  
    // Returns first token 
    char* token = strtok(str, "-"); 
  
    // Keep printing tokens while one of the 
    // delimiters present in str[]. 
    while (token != NULL) { 
        printf("%s\n", token); 
        token = strtok(NULL, "-"); 
    } 
  
    return 0; 
}

输出:


Geeks
for
Geeks

strtok_r()
就像C中的strtok()函数一样,strtok_r()执行将字符串解析为标记序列的相同任务。 strtok_r()是strtok()的可重入版本

有两种方法可以调用strtok_r()

// The third argument saveptr is a pointer to a char * 
// variable that is used internally by strtok_r() in 
// order to maintain context between successive calls
// that parse the same string.
char *strtok_r(char *str, const char *delim, char **saveptr);

下面是一个简单的C程序,用于显示strtok_r()的用法:

// C program to demonstrate working of strtok_r() 
// by splitting string based on space character. 
#include <stdio.h> 
#include <string.h> 
  
int main() 
{ 
    char str[] = "Geeks for Geeks"; 
    char* token; 
    char* rest = str; 
  
    while ((token = strtok_r(rest, " ", &rest))) 
        printf("%s\n", token); 
  
    return (0); 
}

输出:

Geeks
for
Geeks

strtok的另一个示例:

// C code to demonstrate working of 
// strtok 
#include <stdio.h> 
#include <string.h> 
  
// Driver function 
int main() 
{ 
    // Declaration of string 
    char gfg[100] = " Geeks - for - geeks - Contribute"; 
  
    // Declaration of delimiter 
    const char s[4] = "-"; 
    char* tok; 
  
    // Use of strtok 
    // get first token 
    tok = strtok(gfg, s); 
  
    // Checks for delimeter 
    while (tok != 0) { 
        printf(" %s\n", tok); 
  
        // Use of strtok 
        // go through other tokens 
        tok = strtok(0, s); 
    } 
  
    return (0); 
}

输出:

 Geeks
 for
 geeks
 Contribute

实际应用
strtok可用于基于某些分隔符将一个字符串拆分为多个字符串。使用此函数可以实现简单的CSV文件支持。 CSV文件以逗号作为分隔符。

// C code to demonstrate practical application of 
// strtok 
#include <stdio.h> 
#include <string.h> 
  
// Driver function 
int main() 
{ 
    // Declaration of string 
    // Information to be converted into CSV file 
    char gfg[100] = " 1997 Ford E350 ac 3000.00"; 
  
    // Declaration of delimeter 
    const char s[4] = " "; 
    char* tok; 
  
    // Use of strtok 
    // get first token 
    tok = strtok(gfg, s); 
  
    // Checks for delimeter 
    while (tok != 0) { 
        printf("%s, ", tok); 
  
        // Use of strtok 
        // go through other tokens 
        tok = strtok(0, s); 
    } 
  
    return (0); 
}

输出:

1997, Ford, E350, ac, 3000.00,

参考:
1)手册页strtok_r() 2)http://stackoverflow.com/questions/15961253/c-correct-usage-of-strtok-r



相关用法


注:本文由纯净天空筛选整理自 strtok() and strtok_r() functions in C with examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。