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。

实例1

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

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

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

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

子字符串是: NOOB

实例2

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


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

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

实例3

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


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

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值