std::strstr
From cppreference.com
| ヘッダー <cstring> で定義 |
||
| const char* strstr( const char* haystack, const char* needle ); |
||
| char* strstr( char* haystack, const char* needle ); |
||
文字列 haystack の中で、文字列 needle の最初の出現箇所を検索します。終端のヌル文字は比較されません。
目次 |
[編集] パラメータ
| haystack | - | 検査対象のヌル終端バイト文字列へのポインタ |
| needle | - | 検索対象のヌル終端バイト文字列へのポインタ |
[編集] 戻り値
haystack 内で見つかった部分文字列の最初の文字へのポインタ。見つからなかった場合はヌルポインタを返します。 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> の (public メンバ関数) | |
| あるワイド文字列内で、別のワイド文字列が最初に現れる場所を見つける (関数) | |
| 最初に出現する文字を見つける (関数) | |
| 最後に出現する文字を見つける (関数) | |
| C言語のドキュメント (strstr)
| |