.\" Copyright (c) 1990, 1991, 1993 .\" The Regents of the University of California. All rights reserved. .\" .\" This code is derived from software contributed to Berkeley by .\" Chris Torek. .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions .\" are met: .\" 1. Redistributions of source code must retain the above copyright .\" notice, this list of conditions and the following disclaimer. .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. .\" 3. All advertising materials mentioning features or use of this software .\" must display the following acknowledgement: .\" This product includes software developed by the University of .\" California, Berkeley and its contributors. .\" 4. Neither the name of the University nor the names of its contributors .\" may be used to endorse or promote products derived from this software .\" without specific prior written permission. .\" .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" .\" @(#)strsep.3 8.1 (Berkeley) 6/9/93 .\" %FreeBSD: src/lib/libc/string/strsep.3,v 1.6.2.5 2001/12/14 18:33:59 ru Exp % .\" .\" $FreeBSD$ .Dd June 9, 1993 .Dt STRSEP 3 .Os .Sh 名称 .Nm strsep .Nd 文字列の分割 .Sh ライブラリ .Lb libc .Sh 書式 .In string.h .Ft char * .Fn strsep "char **stringp" "const char *delim" .Sh 解説 .Fn strsep 関数は、 .Fa *stringp により参照される文字列の中で、文字列 .Fa delim 中のいずれかの文字 (または終端の .Ql \e0 ) のうちで最初に出現する文字の位置を探し、その箇所を .Ql \e0 で置き換えます。 区切り文字 (または文字列の末尾に達した場合は .Dv NULL ) の次の文字の位置は、 .Fa *stringp に格納されます。 .Fa *stringp の元の値は戻り値となります。 .Pp 「空」のフィールド、すなわち、 2 つの隣接する区切り文字によって生じたフィールドは、 .Fa *stringp の中に返されたポインタが参照する位置を .Ql \e0 と比較することで検出可能です。 .Pp .Fa *stringp が最初から .Dv NULL である場合は、 .Fn strsep は .Dv NULL を返します。 .Sh 例 以下では、 空白文字によって区切られたトークンで構成されている文字列を、 引数ベクタへと解釈するために .Fn strsep を使っています。 .Bd -literal -offset indent char **ap, *argv[10], *inputstring; for (ap = argv; (*ap = strsep(&inputstring, " \et")) != NULL;) if (**ap != '\e0') if (++ap >= &argv[10]) break; .Ed .Sh 関連項目 .Xr memchr 3 , .Xr strchr 3 , .Xr strcspn 3 , .Xr strpbrk 3 , .Xr strrchr 3 , .Xr strspn 3 , .Xr strstr 3 , .Xr strtok 3 .Sh 歴史 .Fn strsep 関数は、 .Fn strtok 関数を置き換えるものとして考えられています。 .Fn strtok 関数は移植性の点では好ましいのですが ( .St -isoC に適合しています) 、 空のフィールドを扱う、すなわち、 2 つの隣接する区切り文字によって区切られたフィールドを検出するとか、 一回で複数の文字列に対して使用する、といったことができません。 .Fn strsep 関数は、 .Bx 4.4 で初めて登場しました。