C 语言 strstr() 函数用于在字符串中查找子串 第一次出现的位置,返回指向该子串的指针。
原型
char *strstr(const char *haystack, const char *needle);参数
haystack- 被查找的主字符串;needle- 要查找的子串。
返回值
返回 haystack 中首次出现 needle 的位置指针;未找到返回 NULL。
示例
#include <stdio.h>
#include <string.h>
int main()
{
char str[] = "The quick brown fox";
char *p = strstr(str, "brown");
if (p != NULL)
printf("找到: %s\n", p);
else
printf("未找到\n");
return 0;
}输出:
找到: brown fox注意事项
strstr 常用于判断一个字符串是否包含另一个字符串(返回值非 NULL 即包含)。