std::strchr
From cppreference.com
| ヘッダー <cstring> で定義 |
||
| const char* strchr( const char* str, int ch ); |
||
| char* strchr( char* str, int ch ); |
||
バイト文字列 str の中で、文字 static_cast<char>(ch) が最初に出現する場所を検索します。
終端のヌル文字も文字列の一部とみなされ、'\0' を検索した場合に見つかります。
目次 |
[編集] パラメータ
| str | - | 解析対象のヌル終端バイト文字列へのポインタ |
| 文字 | - | 検索する文字 |
[編集] 戻り値
見つかった文字へのポインタ。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> の (public メンバ関数) | |
| ワイド文字列内でワイド文字が最初に現れる場所を見つける (関数) | |
| 最後に出現する文字を見つける (関数) | |
| 区切り文字の集合の中からいずれかの文字が最初に出現する位置を見つける (関数) | |
| C documentation for strchr
| |