C 庫函數(shù) - strstr()

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

描述

C 庫函數(shù) char *strstr(const char *haystack, const char *needle) 在字符串 haystack 中查找第一次出現(xiàn)字符串 needle 的位置,不包含終止符 '\0'。

聲明

下面是 strstr() 函數(shù)的聲明。

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

參數(shù)

  • haystack -- 要被檢索的 C 字符串。
  • needle -- 在 haystack 字符串內(nèi)要搜索的小字符串。

返回值

該函數(shù)返回在 haystack 中第一次出現(xiàn) needle 字符串的位置,如果未找到則返回 null。

實例

下面的實例演示了 strstr() 函數(shù)的用法。

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


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

   ret = strstr(haystack, needle);

   printf("子字符串是: %s\n", ret);
   
   return(0);
}

讓我們編譯并運行上面的程序,這將產(chǎn)生以下結果:

子字符串是: School

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