名前空間
変種
操作

strstr

From cppreference.com
< c‎ | string‎ | byte
ヘッダー <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*になります。
  • それ以外の場合、動作は未定義です。
これらの汎用関数のマクロ定義が抑制され、実際の関数にアクセスする場合(例: (strstr) または関数ポインタが使用される場合)、実際の関数宣言 (1) が表示されます。

もし 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

最初に出現する文字を見つける
(関数) [編集]
最後に出現する文字を見つける
(関数) [編集]
English 日本語 中文(简体) 中文(繁體)