strchr
提供: cppreference.com
ヘッダ <string.h> で定義
|
||
char *strchr( const char *str, int ch ); |
||
str
の指すヌル終端バイト文字列 (各文字が unsigned char として解釈されます) 内の ch
((char)ch によって行われたかのように char に変換した後) が現れる最初の位置を探します。 終端のヌル文字は文字列の一部であるとみなされ、 '\0' を検索した場合に見つけられます。
str
がヌル終端バイト文字列を指すポインタでない場合、動作は未定義です。
目次 |
[編集] 引数
str | - | 解析するヌル終端バイト文字列を指すポインタ |
ch | - | 検索する文字 |
[編集] 戻り値
str
内の見つかった文字を指すポインタ、またはそのような文字が見つからなかった場合はヌルポインタ。
[編集] 例
Run this code
#include <stdio.h> #include <string.h> int main(void) { const char *str = "Try not. Do, or do not. There is no try."; char target = 'T'; const char *result = str; while((result = strchr(result, target)) != NULL) { printf("Found '%c' starting at '%s'\n", target, result); ++result; // Increment result, otherwise we'll find target at the same location } }
出力:
Found 'T' starting at 'Try not. Do, or do not. There is no try.' Found 'T' starting at 'There is no try.'
[編集] 参考文献
- C11 standard (ISO/IEC 9899:2011):
- 7.24.5.2 The strchr function (p: 367-368)