名前空間
変種

strstr

提供: cppreference.com
< c | string | byte
<tbody> </tbody>
ヘッダ <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]
Morty Proxy This is a proxified and sanitized view of the page, visit original site.