C-standard-library-c-function-strstr

提供:Dev Guides
移動先:案内検索

Cライブラリ関数-strstr()

説明

Cライブラリ関数 char strstr(const char haystack、const char needle)関数は、文字列 *haystack 内のサブストリング needle の最初の出現を検出します。 終端の「\ 0」文字は比較されません。

宣言

以下は、strstr()関数の宣言です。

char *strstr(const char *haystack, const char *needle)

パラメーター

  • haystack -これはスキャンされるメインのC文字列です。
  • needle -これは、haystack文字列で検索される小さな文字列です。

戻り値

この関数は、needleで指定された文字シーケンス全体のいずれかがhaystack内で最初に現れる場所へのポインターを返します。または、haystackにシーケンスが存在しない場合は、nullポインターを返します。

次の例は、strstr()関数の使用法を示しています。

#include <stdio.h>
#include <string.h>


int main () {
   const char haystack[20] = "finddevguides";
   const char needle[10] = "Point";
   char *ret;

   ret = strstr(haystack, needle);

   printf("The substring is: %s\n", ret);

   return(0);
}

次の結果を生成する上記のプログラムをコンパイルして実行しましょう-

The substring is: Point