文法:
#include <cstring> char *strtok( char *文字列1, const char *文字列2 );
strtok()関数は、文字列2に含まれる文字をトークンの切り方を決める区切り文字として、文字列1に含まれる、次のトークンへのポインタを返します。もしもトークンが含まれていなかった場合にはNULLを返します。文字列をトークンに変換するためには、最初にstrtok()を呼び出す際には、トークン切り出しを行う文字列へのポインタをstr1として渡す必要があります。すべての呼び出しが完了すると、文字列1は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( "結果は \"%s\"\n", result ); result = strtok( NULL, delims ); }
上記のコードを実行すると、下記のような出力が表示されます: The above code will display the following output:
結果は "now " 結果は " is the time for all " 結果は " good men to come to the " 結果は " aid of their country"