Namespaces
Variants

std:: strstr

From cppreference.net
ヘッダーで定義 <cstring>
const char * strstr ( const char * haystack, const char * needle ) ;
char * strstr ( char * haystack, const char * needle ) ;

バイト文字列 needle が指すバイト文字列内で最初に出現する位置を検索します。 haystack 終端のnull文字は比較対象になりません。

目次

パラメータ

haystack - 検査対象のnull終端バイト文字列へのポインタ
needle - 検索対象のnull終端バイト文字列へのポインタ

戻り値

見つかった部分文字列の最初の文字へのポインタを返す。 該当する文字が見つからない場合はヌルポインタを返す。 needle が空文字列を指している場合は、 haystack が返される。

#include <cstring>
#include <iomanip>
#include <iostream>
int main()
{
    const char* str = "Try not. Do, or do not. There is no try.";
    const char* target = "not";
    for (const char* result = str; (result = std::strstr(result, target)); ++result)
        std::cout << "Found " << std::quoted(target)
                  << " starting at (" << result - str << "): "
                  << std::quoted(result) << '\n';
}

出力:

Found "not" starting at (4): "not. Do, or do not. There is no try."
Found "not" starting at (19): "not. There is no try."

関連項目

指定された部分文字列の最初の出現位置を検索する
( std::basic_string<CharT,Traits,Allocator> の公開メンバ関数)
ワイド文字列内で別のワイド文字列の最初の出現位置を検索する
(関数)
文字の最初の出現位置を検索する
(関数)
文字の最後の出現位置を検索する
(関数)