當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。