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 ...
随机推荐
- javascript高级程序设计读书笔记----事件
DOM0级事件处理程序 传统处理方式,即讲一个函数赋值给一个事件处理程序属性. DOM2级事件处理程序 addEventListener()和removeHandler()两个方法用于指定和删 ...
- linux 权限机制
系统每个文件都拥有特定的权限,所属用户及所属组,通过这样的机制限制哪些用户.哪些组可以对特定文件进行什么样的操作,一般分为读.写.执行三种权限. 每个进程都是以某个用户的身份运行,所以进程的权限与该用 ...
- 解决Struts2拦截器的对于参数传递无效问题
今天做项目时,使用拦截器对用户权限检查.拦截器本身没有问题,可是实现权限拦截,但是传递的参数全部都无效了.搞了很久,由于对拦截器的内部机制不是特别熟悉,所以重新研读了一下Struts2的拦截器.找到了 ...
- .net core MVC接受来自自前端的GET和POST请求方法的区别
小伙伴们都知道,常用的http请求有两种方式,即GET方法和POST方法,很多刚入门的童鞋难免都会有一个误区,是不是GET是从服务器上获取数据,POST是向服务器传送数据? 我的理解是:不论是GET还 ...
- javascript 获取iframe里页面中元素值的方法 关于contentWindow和contentDocumen
javascript 获取iframe里页面中元素值的方法 IE方法:document.frames['myFrame'].document.getElementById('test').value; ...
- 小程序:如何在wxml页面中调用JavaScript函数
早上过来遇到一个这样的bug: 在计算百分比的时候没有保留小数点后2位,从而导致一些无法整除的结果显示太长 一开始,我以为这是一个很普通的bug,既然wxml在页面{{}}内支持简单的运算,我想也应该 ...
- ArchLinux中证书错误解决方案
ca-certificates 更新 x509: failed to load system roots and no roots provided. curl error: Problem with ...
- chrome cpu占用100%
参考原文地址:https://stackoverflow.com/questions/20276097/chrome-devtools-100-cpu 问题描述,chrome打开devtools开发者 ...
- sap server笔记
system 就是sap hana database,如果一个system有多个instance,则必须分散到不同的host中,每个system有唯一的sid. hello各位,jackie建议我们去 ...
- Thread类和Runnable接口的比较
Thread和Runnable的联系 Thread类的定义: public class Thread extends Object implements Runnable 联系:从Thread类的定义 ...