名前空間
変種
操作

strstr

提供: cppreference.com
< c‎ | string‎ | byte
ヘッダ <string.h> で定義
char *strstr( const char* str, const char* substr );

str の指すヌル終端バイト文字列内の substr の指すヌル終端バイト文字列が現れる最初の位置を探します。 終端のヌル文字は比較されません。

str または substr がヌル終端バイト文字列を指すポインタでない場合、動作は未定義です。

目次

[編集] 引数

str - 調べるヌル終端バイト文字列を指すポインタ
substr - 検索するヌル終端バイト文字列を指すポインタ

[編集] 戻り値

str 内の見つかった部分文字列の最初の文字を指すポインタ、またはそのような部分文字列が見つからない場合は NULLsubstr が空文字列を指す場合は、 str が返されます。

[編集]

#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

[編集] 参考文献

  • C11 standard (ISO/IEC 9899:2011):
  • 7.24.5.7 The strstr function (p: 369)
  • C99 standard (ISO/IEC 9899:1999):
  • 7.21.5.7 The strstr function (p: 332)
  • C89/C90 standard (ISO/IEC 9899:1990):
  • 4.11.5.7 The strstr function

[編集] 関連項目

文字が現れる最初の位置を探します
(関数) [edit]
文字が現れる最後の位置を探します
(関数) [edit]