strstr
提供: cppreference.com
ヘッダ <string.h> で定義
|
||
char *strstr( const char* str, const char* substr ); |
||
str
の指すヌル終端バイト文字列内の substr
の指すヌル終端バイト文字列が現れる最初の位置を探します。 終端のヌル文字は比較されません。
str
または substr
がヌル終端バイト文字列を指すポインタでない場合、動作は未定義です。
目次 |
[編集] 引数
str | - | 調べるヌル終端バイト文字列を指すポインタ |
substr | - | 検索するヌル終端バイト文字列を指すポインタ |
[編集] 戻り値
str
内の見つかった部分文字列の最初の文字を指すポインタ、またはそのような部分文字列が見つからない場合は NULL。 substr
が空文字列を指す場合は、 str
が返されます。
[編集] 例
Run this code
#include <string.h> #include <stdio.h> void find_str(char const* str, char const* substr) { char* pos = strstr(str, substr); if(pos) { printf("found the string '%s' in '%s' at position: %ld\n", substr, str, pos - str); } else { printf("the string '%s' was not found in '%s'\n", substr, str); } } int main(void) { char* 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