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字符串內要搜索的小字符串。
返回值
該函數返回在haystack 中第一次出現needle 字符串的位置,如果未找到則返回null。
實例
下面的實例演示了strstr() 函數的用法。
#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); }
讓我們編譯並運行上面的程序,這將產生以下結果:
子字符串是: School