名前空間
変種
操作

strchr

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

str の指すヌル終端バイト文字列 (各文字が unsigned char として解釈されます) 内の ch ((char)ch によって行われたかのように char に変換した後) が現れる最初の位置を探します。 終端のヌル文字は文字列の一部であるとみなされ、 '\0' を検索した場合に見つけられます。

str がヌル終端バイト文字列を指すポインタでない場合、動作は未定義です。

目次

[編集] 引数

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

[編集] 戻り値

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

[編集]

#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)
  • C99 standard (ISO/IEC 9899:1999):
  • 7.21.5.2 The strchr function (p: 330)
  • C89/C90 standard (ISO/IEC 9899:1990):
  • 4.11.5.2 The strchr function

[編集] 関連項目

文字が現れる最初の位置を配列から探します
(関数) [edit]
文字が現れる最後の位置を探します
(関数) [edit]
文字列中の任意の文字が別の文字列中に現れる最初の位置を探します
(関数) [edit]