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 ...
随机推荐
- Nginx根据用户请求的不同参数返回不同的json值
用户请求url:http://localhost:8000/getconfig?v=1.03.01,根据参数v=1.03.01或者其他的值返回不同的json值.如果用户请求不带该参数,则返回默认的js ...
- Delphi Cookie获取及使用
以下方法为网上搜集整理,留做备份,随时更新 一:通过URL获取 CanGetIECookie(URL,g_cookie); function CanGetIECookie(const URL: ...
- 基于JSP的B2C的网上拍卖系统_秒杀与竞价-JavaWeb项目-有源码
开发工具:Myeclipse/Eclipse + MySQL + Tomcat 项目简介: 基于B2C的网上拍卖系统主要用于帮助人们应用互联网方便快捷买到自己所中意的商品,并参与到秒杀与竞拍当中.主要 ...
- GridView中文属性
GridControl的中文属性: 1 Appearance 外观 Appearance 外观设置 ColumnFilterButton 行过滤器按钮 BackerColor 背景色 Backe ...
- Android 打开URL中的网页和拨打电话、发送短信功能
拨打电话需要的权限 <uses-permission android:name="android.permission.CALL_PHONE"/> 为了省事界面都写一起 ...
- webstorm中新建vue工程
1.在https://nodejs.org/en/下载安装nodejs 2.vue-cli 搭建框架 首先从官方网站安装 node.js,会一并安装 npm工具.注意 npm一定要3.10以上,以免很 ...
- 在 android 上运行 python 的方法
在android上运行python脚本,或者在android上使用python交互界面,对熟悉python的研究或开发人员来说,是一件很有吸引力的事情,因为python脚本真是非常高效,另外,有很多非 ...
- 原生态js回顶部
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- C# Winform下一个热插拔的MIS/MRP/ERP框架14(自动更新)
对于软件来说,启用自动更新是非常必要的. 根据软件的应用场景,我们可以设计不同的更新模型. 目前,IMES框架运行在.Net framework 4.0下面,使用的Win系统版本在Win7,域内管控, ...
- (USB HID) Configuration Descriptor
最近完成了HID的基本收發,使用的配置用了2個Endpoint,把一些特別重要要的地方紀錄下來 整個Configuration 分成4大部分 : 1. Configuration 2. Interfa ...