C 标准库 - string.h之strstr使用
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;
}
文章参考
- http://www.cplusplus.com/reference/cstring/strstr/
- http://www.runoob.com/cprogramming/c-function-strstr.html
- http://zh.cppreference.com/w/c/string/byte/strstr
转载注明出处
C 标准库 - string.h之strstr使用的更多相关文章
- C 标准库 - string.h
C 标准库 - string.h This header file defines several functions to manipulate C strings and arrays. stri ...
- C标准库<string.h>实现
本文地址:http://www.cnblogs.com/archimedes/p/c-library-string.html,转载请注明源地址. 1.背景知识 <string.h>中声明的 ...
- C标准库string.h中几个常用函数的使用详解
strlen 计算字符串长度 size_t strlen(const char *str) 计算字符串 str 的长度,直到空结束字符,但不包括空结束字符. 函数实现: int Strlen(cons ...
- C 标准库 - string.h之memmove使用
memmove Move block of memory Copies the values of num bytes from the location pointed by source to t ...
- C 标准库 - string.h之memcpy使用
memcpy Copy block of memory Copies the values of num bytes from the location pointed to by source di ...
- C 标准库 - string.h之memcmp使用
memcmp Compare two blocks of memory. Compares the first num bytes of the block of memory pointed by ...
- C 标准库 - string.h之memchr使用
memchr Locate character in block of memory,Searches within the first num bytes of the block of memor ...
- C 标准库 - string.h之strlen使用
strlen Returns the length of the C string str. The length of a C string is determined by the termina ...
- C 标准库 - string.h之strpbrk使用
strpbrk Locate characters in string,Returns a pointer to the first occurrence in str1 of any of the ...
随机推荐
- Java50道经典习题-程序1 不死神兔
题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子对数为多少? 分析:假如:1月1日有1对兔子,根据题目“从出生后 ...
- [LeetCode 题解]: ZigZag Conversion
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 The string ...
- Java中的多态方法
public class Main { public void test(Object o) { System.out.println("Object"); } public vo ...
- Transaction And Lock--常用的查询事务和锁的语句
--===================================================== --查看当前运行事务 SET TRANSACTION ISOLATION LEVEL R ...
- C#操作windows服务,安装、卸载、停止、启动
public class ServiceUtil { private string _ServiceName = string.Empty; private string _AppName = str ...
- .Net Core内存回收模式及性能测试对比
.NET Core 两种GC模式: Server GC / Workstation GC Server GC : 主要应用于多处理器系统,并且作为ASP.NET Core宿主的默认配置.它会为每个处理 ...
- zookeeper 开机启动
第一种:直接修改/etc/rc.d/rc.local文件 在/etc/rc.d/rc.local文件中需要输入两行,其中export JAVA_HOME=/usr/java/jdk1.8.0_112是 ...
- redis安装 卸载 启动 关闭
一 redis安装 第一步:在VMware中安装CentOS(参考Linux教程中的安装虚拟机) 第二步:在Linux下安装gcc环境 [root@hadoop ~]#yum install gcc- ...
- 如何安全地运行用户的 JavaScript 脚本
本文来自网易云社区,转载务必请注明出处. 有时候我们需要运行用户输入的 JavaScript 脚本(以下简称脚本).对于我们来说,这些脚本是不可信任的,如果在当前的 Context 中运行这些脚本,它 ...
- OCP认证052新加的考试题及答案整理-21
21.Which two are true about roles? A) A role can be password-protected. B) A role can be granted to ...