C 标准库 - string.h之strrchr使用
strrchr
- Locate last occurrence of character in string, Returns a pointer to the last occurrence of character in the C string str.
- The terminating null-character is considered part of the C string. Therefore, it can also be located to retrieve a pointer to the end of a string.
- 寻找 ch (如同用 (char)ch 转换到 char 后)在 str 所指向的空终止字节串中(将每个字符转译成 unsigned char )的最后出现。若搜索 '\0' ,则认为终止空字符为字符串的一部分,而且能找到。
- C 库函数 char *strrchr(const char *str, int c) 在参数 str 所指向的字符串中搜索最后一次出现字符 c(一个无符号字符)的位置。
- 若 str 不是指向空终止字节串的指针,则行为未定义。
char *strrchr( const char *str, int ch );
Parameters
str
- C string.
- 指向要分析的空终止字节字符串的指针
character
- Character to be located. It is passed as its int promotion, but it is internally converted back to char.
- 要搜索的字符
- 要搜索的字符。以 int 形式传递,但是最终会转换回 char 形式。
Return Value
- A pointer to the last occurrence of character in str.If the character is not found, the function returns a null pointer.
- 指向 str 中找到的字符的指针,或若找不到这种字符则为空指针。
- 该函数返回 str 中最后一次出现字符 c 的位置。如果未找到该值,则函数返回一个空指针。
Example
//
// Created by zhangrongxiang on 2018/2/6 9:12
// File strrchr
//
#include <stdio.h>
#include <string.h>
//该函数返回 str 中最后一次出现字符 c 的位置。如果未找到该值,则函数返回一个空指针。
int main() {
const char str[] = "https://github.com/zhangrxiang/learn-c";
const char str2[] = "D:\\WorkSpace\\clionProjects\\learn-c\\string\\strrchr.c";
const char ch = '/';
const char ch2 = '\\';
char *ret;
ret = strrchr(str, ch);
// |/| ------ |/learn-c|
printf("|%c| ------ |%s|\n", ch, ret);
printf("%c\n", ret[0]);// /
printf("%ld\n", ret - str + 1);//number 31
printf("%ld\n", ret - str);//index 30
ret = strrchr(str2, ch2);
// |\| ------ |\strrchr.c|
printf("|%c| ------ |%s|\n", ch2, ret);
printf("%c\n", ret[0]); /*** \ */
printf("%c\n", str2[ret - str2]);/*** \ */
printf("%ld\n", ret - str2 + 1);//42
printf("%d\n", (int) strlen(ret));//10
printf("%s\n", ret + 1);//strrchr.c
printf("%c\n", *ret); /*** \ */
printf("%c\n", *(ret + sizeof(char)));//s
printf("%c\n", *(ret + sizeof(char) * 2));//t
printf("%s\n", &(*ret));// \strrchr.c
printf("%s\n", &*(ret + sizeof(char)));//strrchr.c
ret = strrchr(str,'A');
if (ret){
printf("exists A\n");
} else{
// no exists A
printf("no exists A\n");
}
return 0;
}
参考文章
- http://www.cplusplus.com/reference/cstring/strrchr/
- http://zh.cppreference.com/w/c/string/byte/strrchr
- http://www.runoob.com/cprogramming/c-function-strrchr.html
转载注明出处
C 标准库 - string.h之strrchr使用的更多相关文章
- 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之strpbrk使用
strpbrk Locate characters in string,Returns a pointer to the first occurrence in str1 of any of the ...
- 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 ...
随机推荐
- 介绍自己以及github注册流程
我叫何季生,来自网络工程141,学号是1413042027,我喜欢看一些动漫和游戏,对于编程并不是很厉害希望今年能够有所突破. github注册流程:在刚开始注册github时,我用的是qq浏览器,却 ...
- android之实现底部TabHost
先说布局文件,如下:利用android:layout_alignParentBottom="true" 实现底部显示 <?xml version="1.0" ...
- docker+selenium Grid搭建自动化分布式测试环境
自动化测试需要考虑到兼容性的时候,之前的做法是每个执行机上安装不同版本的浏览器,实际上这样做会很浪费硬件资源,现在有了docker容器化技术,让一切变得简单. 工具清单: 语言:python 2.7 ...
- 按照已有的模板打印小票<二> ——调用windows打印机打印 可设置字体样式
按照已有的模板打印小票<二> ——调用windows打印机打印 可设置字体样式 之前写过一篇文章<按照已有的模板输出一(如发票)>,是关于如何给已有的模板赋值.在项目的实践过程 ...
- 长按tools Icon 弹出Tips音效
快速点击,还没弹出tips,bubble音效已播放 在 Widget_ToolsTips 的 OnAwake 函数加一个延时 transform:DOScale(1, 0.1):OnComlete(f ...
- Android 虚拟机访问本地服务器的地址
http://10.0.2.2:8080/test/ 比如 localhost:8080 为 tomcat 地址,然后 test 是 tomcat 目录下 webapps 的子目录. 10.0.2.2 ...
- [TJOI2007] 线段
因为每行必须走完才能到下一行,所以我们有两种决策: 1.最后留在线段左端点 2.最后留在线段右端点 这种存在状态转移且多决策的问题用动态规划来进行递推是最好不过的了. 所以我们设\(dp[i][0/1 ...
- DAY31、socket套接字
一.复习1.网络编程 软件开发架构 b/s架构 c/s架构 本质都是c/s架构2.互联网协议 OSI七层协议 应用层 表示层 会话层 传输层 网络层 数据链路层 物理连接层3. 物理连接层:建立物理连 ...
- 爬虫开发11.scrapy框架之CrawlSpider操作
提问:如果想要通过爬虫程序去爬取”糗百“全站数据新闻数据的话,有几种实现方法? 方法一:基于Scrapy框架中的Spider的递归爬取进行实现(Request模块递归回调parse方法). 方法二:基 ...
- 发布 Android Library 到 JCenter 从入门到放弃
最近想倒腾一个小小的 UIKit 到 JCenter,为开源社区贡献一点绵薄之力,于是就有了一系列惨无人道的踩坑史.好,接下来,直奔主题,以下是发布流程. 发布到 JCenter 发布到 JCente ...