C++ 程式設計/程式碼/標準 C 庫/函式/strtok
外觀
| 語法 |
#include <cstring>
char *strtok( char *str1, const char *str2 );
|
strtok() 函式返回 str1 中下一個 "token" 的指標,其中 str2 包含確定 token 的分隔符。如果未找到 token,strtok() 返回 NULL。為了將字串轉換為 token,對 strtok() 的第一次呼叫應讓 str1 指向要標記化的字串。所有後續呼叫應讓 str1 為 NULL。
例如
char str[] = "now # is the time for all # good men to come to the # aid of their country";
char delims[] = "#";
char *result = NULL;
result = strtok( str, delims );
while( result != NULL ) {
printf( "result is \"%s\"\n", result );
result = strtok( NULL, delims );
}
上面的程式碼將顯示以下輸出
result is "now " result is " is the time for all " result is " good men to come to the " result is " aid of their country"