strcspn() — 最初に一致した文字のオフセットの検索
フォーマット
#include <string.h>
size_t strcspn(const char *string1, const char *string2);
言語レベル
ANSI
スレッド・セーフ
はい
ロケール依存
この関数の振る舞いは、現行ロケールの LC_CTYPE カテゴリーの影響を受ける可能性があります。詳細については、CCSID およびロケールの理解を参照してください。
説明
strcspn() 関数は、string2 で指定された一連の文字に属する string1 内の文字の最初の出現を検出します。 ヌル文字は、検索対象になりません。
strcspn() 関数は、ヌル終了ストリング上で作動します。関数のストリング引数には、 ストリングの終わりを示すマークであるヌル文字 (\0) が含まれていなければなりません。
戻り値
strcspn() 関数は、検出された最初の文字のインデックスを戻します。 この値は、すべて string2 内にはない文字で構成される、string1 の最初のサブストリングの長さと同じです。
例
この例では strcspn() を使用して、"string" で
"a"、"x"、"l"、または e のいずれかの文字が最初に現れる位置を検索します。
#include <stdio.h>
#include <string.h>
#define SIZE 40
int main(void)
{
char string[SIZE] = "This is the source string";
char * substring = "axle";
printf( "The first %i characters in the string ¥"%s¥" "
"are not in the string ¥"%s¥" ¥n",
strcspn(string, substring), string, substring);
}
/********** Output should be similar to: **************
The first 10 characters in the string "This is the source string"
are not in the string "axle"
*/