strtok

Section: C Library Functions (3)
Updated: 2026-02-25
Index JM Home Page roff page
 

名前

strtok - extract tokens from strings  

ライブラリ

標準 C ライブラリ (libc,~-lc)  

書式

#include <string.h>

char *strtok(char *_Nullable restrict str, const char *restrict delim);
 

説明

The strtok() function breaks a string into a sequence of zero or more nonempty tokens. On the first call to strtok(), the string to be parsed should be specified in str. In each subsequent call that should parse the same string, str must be NULL.

delim 引数には、解析対象の文字列をトークンに区切るのに使用する バイト集合を指定する。同じ文字列を解析する一連の呼び出しにおいて、 delim に違う文字列を指定してもよい。

strtok() のそれぞれの呼び出しでは、次のトークンを格納したヌル終端 された文字列へのポインターが返される。この文字列には区切りバイトは含まれ ない。これ以上トークンが見つからなかった場合には、NULL が返される。

同じ文字列に対して操作を行う strtok() を連続して呼び出す場合、 次のトークンを探し始める位置を決めるためのポインターが保持される。 最初の strtok の呼び出しでは、 このポインターは対象の文字列の最初のバイトにセットされる。 次のトークンの先頭は、 str 内で次の区切りバイト以外のバイトを前方に検索して決定される。 区切りバイト以外のバイトが見つからなかった場合は、 トークンはこれ以上なく、 strtok() は NULL を返す (したがって、 空の文字列や区切りバイトだけを含む文字列の場合には、 最初の strtok() の呼び出しで NULL が返ることになる)。

The end of each token is found by scanning forward until either the next delimiter byte is found or until the terminating null byte ('\0') is encountered. If a delimiter byte is found, it is overwritten with a null byte to terminate the current token, and strtok() saves a pointer to the following byte; that pointer will be used as the starting point when searching for the next token. In this case, strtok() returns a pointer to the start of the found token.

From the above description, it follows that a sequence of two or more contiguous delimiter bytes in the parsed string is considered to be a single delimiter, and that delimiter bytes at the start or end of the string are ignored. Put another way: the tokens returned by strtok() are always nonempty strings. Thus, for example, given the string "aaa;;bbb,", successive calls to strtok() that specify the delimiter string ";," would return the strings "aaa" and "bbb", and then a null pointer.  

返り値

strtok() returns a pointer to the next token, or NULL if there are no more tokens.  

属性

この節で使用されている用語の説明については、 attributes(7) を参照。
インターフェース属性値
strtok() Thread safetyMT-Unsafe race:strtok
 

標準規格

C11, POSIX.1-2008.  

歴史

POSIX.1-2001, C89, SVr4, 4.3BSD.  

警告

この関数を使う時は注意すること。 もし使うなら、以下のことに注意すること。
•
区切りバイト自体は失われてしまう。
•
The strtok() function uses a static buffer while parsing, so it's not thread safe. Use strtok_r(3) if this matters to you.
 

例

See getaddrinfo_a(3).  

関連項目

memchr(3), strchr(3), string(3), strpbrk(3), strsep(3), strspn(3), strstr(3), strtok_r(3), wcstok(3)


 

Index

名前
ライブラリ
書式
説明
返り値
属性
標準規格
歴史
警告
例
関連項目

This document was created by man2html, using the manual pages.