名前空間
変種
操作

std::strchr

提供: cppreference.com
< cpp‎ | string‎ | byte
ヘッダ <cstring> で定義
constchar* strchr(constchar* str, int ch );
      char* strchr(       char* str, int ch );

str の指すバイト文字列内の文字 static_cast<char>(ch) が現れる最初の位置を探します。

終端のヌル文字は文字列の一部であるとみなされ、 '\0' を検索した場合に見つけられます。

目次

[編集]引数

str - 解析するヌル終端バイト文字列を指すポインタ
ch - 検索する文字

[編集]戻り値

str 内の見つかった文字を指すポインタ、またはそのような文字が見つからなかった場合はヌルポインタ。

[編集]

#include <iostream>#include <cstring>   int main(){constchar*str ="Try not. Do, or do not. There is no try.";char target ='T';constchar*result = str;   while((result = std::strchr(result, target))!=NULL){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.'

[編集]関連項目

文字が現れる最初の位置を配列から探します
(関数)[edit]
文字列内の文字を探します
(std::basic_string<CharT,Traits,Allocator>のパブリックメンバ関数)[edit]
ワイド文字列中のワイド文字が現れる最後の位置を探します
(関数)[edit]
文字が現れる最後の位置を探します
(関数)[edit]
区切り文字集合の任意の文字が文字列中に現れる最初の位置を探します
(関数)[edit]
close