Namespaces
Variants

std:: strchr

From cppreference.net
ヘッダーで定義 <cstring>
const char * strchr ( const char * str, int ch ) ;
char * strchr ( char * str, int ch ) ;

バイト文字列 str が指す先で、文字 static_cast < char > ( ch ) の最初の出現を検索します。

終端ナル文字は文字列の一部と見なされ、 ' \0 ' を検索することで見つけることができます。

目次

パラメータ

str - 解析対象のヌル終端バイト文字列へのポインタ
ch - 検索対象の文字

戻り値

見つかった文字へのポインタを str 内で返します。該当する文字が見つからない場合はヌルポインタを返します。

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

出力:

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

関連項目

配列内で最初に現れる文字を検索する
(関数)
指定された部分文字列の最初の出現位置を検索する
( std::basic_string<CharT,Traits,Allocator> の公開メンバ関数)
ワイド文字列内で最初に現れるワイド文字を検索する
(関数)
最後に現れる文字を検索する
(関数)
区切り文字セット内の任意の文字が最初に現れる位置を検索する
(関数)