C 庫函數 - strstr()

C 標準庫 - <string.h> C 標準庫 - <string.h>

描述

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 字串內要搜索的小字符串。

返回值

該函數返回在 haystack 中第一次出現 needle 字串的位置,如果未找到則返回 null。

實例

下麵的實例演示了 strstr() 函數的用法。

實例

#include <stdio.h> #include <string.h> int main() { const char haystack[20] = "zaixian"; const char needle[10] = "NOOB"; char *ret; ret = strstr(haystack, needle); printf("子字串是: %s\n", ret); return(0); }

讓我們編譯並運行上面的程式,這將產生以下結果:

子字串是: NOOB

C 標準庫 - <string.h> C 標準庫 - <string.h>