C 库函数 - strstr()

C 语言教程 · 2019-02-24 17:27:34

描述

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] = "CODERCTO";
   const char needle[10] = "NOOB";
   char *ret;

   ret = strstr(haystack, needle);

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

让我们编译并运行上面的程序,这将产生以下结果:

子字符串是: NOOB

点击查看所有 C 语言教程 文章: https://codercto.com/courses/l/17.html

查看所有标签

Usability for the Web

Usability for the Web

Tom Brinck、Darren Gergle、Scott D. Wood / Morgan Kaufmann / 2001-10-15 / USD 65.95

Every stage in the design of a new web site is an opportunity to meet or miss deadlines and budgetary goals. Every stage is an opportunity to boost or undercut the site's usability. Thi......一起来看看 《Usability for the Web》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具