strstr
From cppreference.com
| ヘッダー <string.h> で定義 |
||
| char* strstr( const char* str, const char* substr ); |
(1) | |
| /*QChar*/* strstr( /*QChar*/* str, const char* substr ); |
(2) | (C23以降) |
1) 末尾にヌル文字を持つバイト文字列 substr が、末尾にヌル文字を持つバイト文字列 str の中で最初に出現する場所を探します。末尾のヌル文字は比較されません。
2) 上記 (1) の型汎用(type-generic)関数です。
T は未修飾の文字型とします。strの型がconst T*の場合、戻り値の型はconst char*になります。- それ以外の場合、
strの型がT*の場合、戻り値の型はchar*になります。 - それ以外の場合、動作は未定義です。
もし str または substr が末尾にヌル文字を持つバイト文字列へのポインタでない場合、動作は未定義です。
目次 |
[編集] Parameters
| str | - | 検索対象の、末尾にヌル文字を持つバイト文字列へのポインタ |
| substr | - | 検索する、末尾にヌル文字を持つバイト文字列へのポインタ |
[編集] Return value
str 内で見つかった部分文字列の最初の文字へのポインタ、またはそのような部分文字列が見つからなかった場合はヌルポインタ。もし substr が空文字列を指す場合、str が返されます。
[編集] Example
このコードを実行
#include <stdio.h> #include <string.h> void find_str(char const* str, char const* substr) { char const* pos = strstr(str, substr); if (pos) printf( "Found the string [%s] in [%s] at position %td\n", substr, str, pos - str ); else printf( "The string [%s] was not found in [%s]\n", substr, str ); } int main(void) { char const* str = "one two three"; find_str(str, "two"); find_str(str, ""); find_str(str, "nine"); find_str(str, "n"); return 0; }
出力
Found the string [two] in [one two three] at position 4 Found the string [] in [one two three] at position 0 The string [nine] was not found in [one two three] Found the string [n] in [one two three] at position 1
[編集] References
- C23標準 (ISO/IEC 9899:2024)
- 7.24.5.7 The strstr function (p: TBD)
- C17標準 (ISO/IEC 9899:2018)
- 7.24.5.7 The strstr function (p: 269)
- C11標準 (ISO/IEC 9899:2011)
- 7.24.5.7 The strstr function (p: 369)
- C99標準 (ISO/IEC 9899:1999)
- 7.21.5.7 The strstr function (p: 332)
- C89/C90標準 (ISO/IEC 9899:1990)
- 4.11.5.7 The strstr function
[編集] See also
| 最初に出現する文字を見つける (関数) | |
| 最後に出現する文字を見つける (関数) | |
| C++ドキュメント for strstr
| |