strstr

  • Returns a pointer to the first occurrence of str2 in str1, or a null pointer if str2 is not part of str1.
  • 查找 substr 所指的空终止字节字符串在 str 所指的空终止字节字符串中的首次出现。不比较空终止字符。
  • 若 str 或 substr 不是指向空终止字节字符串的指针,则行为未定义。
char *strstr(const char *haystack, const char *needle)

Parameters

haystack

  • C string to be scanned.
  • 要被检索的 C 字符串。

needle

  • C string containing the sequence of characters to match.
  • 在 haystack 字符串内要搜索的小字符串。

Return Value

  • A pointer to the first occurrence in str1 of the entire sequence of characters specified in str2, or a null pointer if the sequence is not present in str1.
  • 该函数返回在 haystack 中第一次出现 needle 字符串的位置,如果未找到则返回 null。

Example

//
// Created by zhangrongxiang on 2017/8/15 14:15.
// Copyright (c) 2017 ZRC . All rights reserved.
// //#include <syslib.h>
#include <string.h>
#include <stdio.h> //C 库函数 char *strstr(const char *haystack, const char *needle) 在字符串 haystack 中查找第一次出现字符串 needle 的位置,不包含终止符 '\0'。 void find_str(char const *str, char const *substr) {
char *pos = strstr(str, substr);
if (pos) {
printf("found the string '%s' in '%s' at position: %ld; result: %s\n", substr, str, pos - str, pos);
} else {
printf("the string '%s' was not found in '%s'\n", substr, str);
}
} int main() {
char *s = "GoldenGlobalView";
char *l = "lob";
char *p;
//返回值:若str2是str1的子串,则返回str2在str1的首次出现的地址;
// 如果str2不是str1的子串,则返回NULL。
p = strstr(s, l);
if (p)
//lobalView
printf("%s\n", p);
else
printf("NotFound!\n"); char *str = "one two three";
find_str(str, "two"); //found the string 'two' in 'one two three' at position: 4; result: two three
find_str(str, ""); // found the string '' in 'one two three' at position: 0; result: one two three
find_str(str, "nine");// the string 'nine' was not found in 'one two three'
find_str(str, "n"); // found the string 'n' in 'one two three' at position: 1; result: ne two three char str2[] = "This is a simple string";
char *pch;
pch = strstr(str2, "simple");
strncpy (pch, "sample", 6);
puts(str2);//This is a sample string
puts(pch);//sample string return 0;
}

文章参考

转载注明出处

C 标准库 - string.h之strstr使用的更多相关文章

  1. C 标准库 - string.h

    C 标准库 - string.h This header file defines several functions to manipulate C strings and arrays. stri ...

  2. C标准库<string.h>实现

    本文地址:http://www.cnblogs.com/archimedes/p/c-library-string.html,转载请注明源地址. 1.背景知识 <string.h>中声明的 ...

  3. C标准库string.h中几个常用函数的使用详解

    strlen 计算字符串长度 size_t strlen(const char *str) 计算字符串 str 的长度,直到空结束字符,但不包括空结束字符. 函数实现: int Strlen(cons ...

  4. C 标准库 - string.h之memmove使用

    memmove Move block of memory Copies the values of num bytes from the location pointed by source to t ...

  5. C 标准库 - string.h之memcpy使用

    memcpy Copy block of memory Copies the values of num bytes from the location pointed to by source di ...

  6. C 标准库 - string.h之memcmp使用

    memcmp Compare two blocks of memory. Compares the first num bytes of the block of memory pointed by ...

  7. C 标准库 - string.h之memchr使用

    memchr Locate character in block of memory,Searches within the first num bytes of the block of memor ...

  8. C 标准库 - string.h之strlen使用

    strlen Returns the length of the C string str. The length of a C string is determined by the termina ...

  9. C 标准库 - string.h之strpbrk使用

    strpbrk Locate characters in string,Returns a pointer to the first occurrence in str1 of any of the ...

随机推荐

  1. Delphi XE8 iOS与Android移动应用开发(APP开发)教程[完整中文版]

    https://item.taobao.com/item.htm?id=536584650957&toSite=main

  2. Windows下Mongodb安装及配置(转载)

    转载(https://blog.csdn.net/liang377122210/article/details/79062681) MongoDB的安装很简单,设置好安装路径后,一直Next直到安装结 ...

  3. Java50道经典习题-程序4 分解质因数

    题目:将一个正整数分解质因数.例如:输入90,打印出90=2*3*3*5.分析:对n进行分解质因数,应先找到一个最小的质数k,然后按下述步骤完成:(1)如果这个质数恰等于n,则说明分解质因数的过程已经 ...

  4. 系统数据库--恢复Master数据库

    实现步骤:关闭SQL SERVER 服务,使用DAC登录 在cmd下还原master 重启SQL SERVER 服务

  5. Ubuntu 安装 Memcached

    直接使用命令 sudo apt-get install Memcached 进行安装 安装完, 默认只能本地连接,需要修改一下配制文件,打开 /etc/Memcached.conf 文件, 找到一个 ...

  6. Python 振动分析 迭代法计算高阶特征值及特征向量

    参考书 : <<振动分析>> 张准 汪凤泉 编著 东南大学出版社 ISBN 7-80123-583-4 参考章节 : 4.6.2 和 4.6.3 <<数值分析> ...

  7. bzoj1059矩阵游戏

    题目链接 矩阵快速幂+二分图匹配, 对于对角线上的每个点看看能不能换到就行, 但是一开始$dicnic$写挂了 只好写的匈牙利 /*********************************** ...

  8. Archlinux下virtualbox报错'/sbin/rcvboxdrv setup'

    因为刚刚换成archlinux系统,安装virtualbox的时候报错了.如下图: 可是怎么解决呢?我看了很多资料大多数是ubuntu的,没有archlinux的. 但是原理都差不多我借着也就研究出来 ...

  9. 正则表达式,sed简单用法

      一. 正则表达式 1. 常见的正则表达式字符 [] 匹配字符集 grep "bl[lo]g" oldboy.txt 表示字符‘l’或者‘o’都可匹配 * 重复前面字符任意次 g ...

  10. springboot 搭建 简单 web项目 【springboot + freemark模板 + yml 配置文件 + 热修复 + 测试用例】附源码

    项目 地址:  https://gitee.com/sanmubird/springboot-simpleweb.git 项目介绍: 本项目主要有一下内容: 1: springboot yml 配置 ...