C语言字符篇(四)字符串查找函数
#include <string.h> | |
char *strchr(const char *s, int c);
|
The strchr() function returns a pointer to the first occurrence of the character c in the string s.
|
char *strrchr(const char *s, int c); | The strrchr() function returns a pointer to the last occurrence of the character c in the string s. |
size_t strspn(const char *s, const char *accept); | The strspn() function calculates the length (in bytes) of the initial segment of s which consists entirely of bytes in accept. |
size_t strcspn(const char *s, const char *reject); | The strcspn() function calculates the length of the initial segment of s which consists entirely of bytes not in reject. |
char *strpbrk(const char *s, const char *accept); | The strpbrk() function returns a pointer to the byte in s that matches one of the bytes in accept, or NULL if no such byte is found. |
char *strstr(const char *haystack, const char *needle); | These functions return a pointer to the beginning of the located substring, or NULL if the substring is not found. |
- int main(int argc, char **argv) {
- const char *buf="hello strchr";
- char *p1;
- char *p2;
- p1=strchr(buf,'l'); //记录字符l第一次出现的位置,并范围第一次出现该字符的指针
- printf("%s\n",p1); //llo strchr
- p2=strrchr(buf,'l');//记录字符最后一次出现的位置,并范围第一次出现该字符的指针
- printf("%s\n",p2); //lo strchr
- }
- int main(int argc, char **argv) {
- const char *buf="hello world";
- int len;
- /*在buf中寻找buf2中的任意字符,也就是说,在buf中,如果碰到buf2中的任意字符,就结束寻找,并记录之前不相等部分的字符数*/
- /*比如,在buf中有个空格是buf2可以匹配到的,在空格之前有5个字节是不匹配的,所以返回值为5*/
- /*统计不同的数,直到出现了buf2中存在的字符*/
- len=strcspn(buf,"\t\n,.?! "); //
- printf("scpn:%d\n",len);
- /*统计相同的数,直到出现了buf2里面不存在的字符*/
- len=strspn(buf,"abcdefghijklmn"); //hell 都在buf2中出现过,所以开始统计,到 o,buf2中没有,返回到停止之前统计的字符数
- printf("spn:%d\n",len);
- }
- int main(int argc, char **argv) {
- const char *buf="hello,kmist";
- char *p;
- p = strpbrk(buf,"abcdefg");
- printf("%s\n",p); //ello,kmist
- }
- int main(int argc, char **argv) {
- const char *buf="hello,kmist";
- char *p;
- p = strstr(buf,"kmi");
- printf("%s\n",p); //kmist ,如果没有就返回null
- }
C语言字符篇(四)字符串查找函数的更多相关文章
- C/C++字符串查找函数
C/C++ string库(string.h)提供了几个字符串查找函数,如下: memchr 在指定内存里定位给定字符 strchr 在指定字符串里定位给定字符 strcspn 返回在字符串str1里 ...
- C/C++字符串查找函数 <转>
C/C++ string库(string.h)提供了几个字符串查找函数,如下: memchr 在指定内存里定位给定字符 strchr 在指定字符串里定位给定字符 strcspn 返回在字符串str1里 ...
- php字符串查找函数 php查找字符串中出现的次数函数substr_count,判断字符串中是否包含另一个字符串函数strpos
php字符串查找函数 php查找字符串中出现的次数函数substr_count,判断字符串中是否包含另一个字符串函数strpossubstr_count($haystack, $needle [,$o ...
- php中常用的字符串查找函数strstr()、strpos()实例解释
string strstr ( string $haystack , mixed $needle [, bool $before_needle = false ] ) 1.$haystack被查找的字 ...
- C语言字符篇(二)字符串处理函数
字符串处理函数 1. 拷贝 strcpy 2. 追加 strcat #include <string.h> char *strcpy(char *dest, const char ...
- C语言中常用的字符串处理函数总结
C语言中字符串处理函数备注 此文仅用于自己研究和记录 字符串处理函数 1. char *gets(char *s); #include<stdio.h> 功能: 从标准输入读入字符,并保存 ...
- c语言字符数组与字符串的使用详解
转自:http://www.jb51.net/article/37456.htm 1.字符数组的定义与初始化字符数组的初始化,最容易理解的方式就是逐个字符赋给数组中各元素.char str[10]={ ...
- C语言中常用的字符串操作函数
程序开头要声明 #include <string.h> 函数名: stpcpy 功 能: 拷贝一个字符串到另一个 用 法: char *stpcpy(char *destin, char ...
- Excel怎样从一串字符中的某个指定“字符”前后截取字符及截取字符串常用函数
怎么样可以从一串字符中的某个指定位置的前或后截取指定个数的字符. 如:12345.6789,我要截取小数点前(或后)的3个字符.怎么样操作, 另外,怎么样从右边截取字符,就是和left()函数相反的那 ...
- C语言字符数组和字符串
用来存放字符的数组称为字符数组,例如: char a[10]; //一维字符数组 char b[5][10]; //二维字符数组 char c[20]={'c', ' ', 'p', 'r', 'o' ...
随机推荐
- ACM-单调队列
对于单调队列的基本概念可以去看百科里的相关介绍:http://baike.baidu.com/view/3771451.htm 这里挑一些重点. 作用: 不断地向缓存数组里读入元素,也不时地去掉最老的 ...
- nopCommerce 3.9 中文语言包
点击下载:nopCommerce 3.9 中文语言包 nopCommerce39语言包.xml 下载后解压通过后台导入即可使用.如何导入?点击这里
- Java方法命名之“由简入繁”原则
1.访问控制层(Controller 层)中的方法命名方向是简洁明了,向着自然化语言方向靠拢,比如“更新用户”,建议命名为“updateUser”,而非“updateUserById”,实际上我们更新 ...
- 让你迅速了解redis
(1)什么是redis? Redis 是一个基于内存的高性能key-value数据库. (2)Reids的特点 Redis本质上是一个Key-Value类型的内存数据库,很像memcached,整个数 ...
- sharepoint 2010 FieldLookup 的更新方法
网上有,但是是错误的,稍微改一下,就可以了 第一个方法是要更新的item,其中 _fieldname 为要更新的lookup字段名字,_lookupValue为要更新的值. public void S ...
- java:错误Error,异常Excepition
java中throw异常后代码还会继续执行吗 今天遇到一个问题,在下面的代码中,当抛出运行时异常后,后面的代码还会执行吗,是否需要在异常后面加上return语句呢? public void add(i ...
- php的yii框架开发总结5
MVC架构之model类: 我的日报系统用到的数据表:tbl_dailyreport表 其中anthor_id是外键,对应tbl_user数据表的主键id,下面是tbl_user表 class Dai ...
- 分享一个JDK1.8丢失数字精度的案例
差异出现在 DigitList.java的 round() 方法处理上: 1.6: 1.8: 根据设置规则消除无需显示的数字时,JDK1.8 新增了一个二进制数向ASCII码转换的过程如下: 从而导致 ...
- 浏览器-http协议简介
HTTP简介 HTTP协议是Hyper Text Transfer Protocol(超文本传输协议)的缩写,是用于从万维网(WWW:World Wide Web )服务器传输超文本到本地浏览器的传送 ...
- SQL Server 2008数据备份与还原的原理是什么?
为什么SqlServer有完整备份.差异备份和事务日志备份三种备份方式,以及为什么数据库又有简单模式.完整模式和大容量日志模式这三种恢复模式.本文内容适用于2005以上所有版本的SqlServer数据 ...