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