名前空間
変種
操作

std::strstr

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

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

目次

[編集] 引数

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

[編集] 戻り値

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

[編集]

#include <iostream>
#include <cstring>
 
int main()
{
    const char *str = "Try not. Do, or do not. There is no try.";
    const char *target = "not";
    const char *result = str;
 
    while ((result = std::strstr(result, target)) != NULL) {
        std::cout << "Found '" << target 
                  << "' starting at '" << result << "'\n";
 
        // Increment result, otherwise we'll find target at the same location
        ++result;
    }   
}

出力:

Found 'not' starting at 'not. Do, or do not. There is no try.'
Found 'not' starting at 'not. There is no try.'

[編集] 関連項目

文字列内の文字を探します
(std::basic_string<CharT,Traits,Allocator>のパブリックメンバ関数) [edit]
別のワイド文字列中のワイド文字列が現れる最初の位置を探します
(関数) [edit]
文字が現れる最初の位置を探します
(関数) [edit]
文字が現れる最後の位置を探します
(関数) [edit]