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 ...
随机推荐
- Transcation And Lock--SQL SERVER 事务隔离级别
SQL SERVER 事务隔离级别:1.未提交读(READ UNCOMMITED) 允许脏读,读取数据时不加共享锁,与使用WITH(NOLOCK)结果相同2.已提交读 不允许脏读,读取数据 ...
- Robot Framework资料
https://www.cnblogs.com/pachongshangdexuebi/category/981644.html 虫师 :http://www.cnblogs.com/fnng/ ...
- SqlServer发布订阅
我们在开发系统的时候,经常会遇到高并发的问题,还有高可用性和安全性方面的考虑,需要用读写分离的方案来解决问题.也就是在我们使用数据库比较多,更新少而查询比较多的情况下使用读写分离,实现提高性能,减少数 ...
- Oracle彻底卸载
Oracle彻底卸载 卸载:oracle卸载1.删除注册表:打开注册表:regedit 打开路径: <找注册表 :开始->运行->regedit> HKEY_LOCAL_MAC ...
- Java反射机制与工厂模式
工厂模式属于创建型模式,它提供了一种创建对象的最佳方式. 它的特点是:客户端的程序类不直接牵扯到对象的实例化管理,只与接口发生关联,通过工厂类获取指定接口的实例化对象. 简单工厂模式如下: inter ...
- “全栈2019”Java第四十一章:static关键字
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...
- luoguP2479 [SDOI2010]捉迷藏
https://www.luogu.org/problemnew/show/P2479 据说可以用线段树做但是我不会,只能写一个 KD-Tree 了 对于每个点求出距离它最远的点和最近的点的距离,然后 ...
- [CF700E][JZOJ5558]Cool Slogan (后缀自动机+线段树)
题意翻译 给出一个长度为$n$的字符串$s[1]$,由小写字母组成.定义一个字符串序列$s[1....k]$,满足性质:$s[i]$在$s[i-1]$ $(i>=2)$中出现至少两次(位置可重叠 ...
- ArchLinux 下文件描述符
stderr -> /proc/self/fd/2 标准错误:2 stdin -> /proc/self/fd/0 标准输入:0 stdout -> /proc/self/fd/1 ...
- jqury属性操作,特殊效果
一. 常用属性操作 1.html() 取出或设置html内容 // 取出html内容 var $htm = $('#div1').html(); // 设置html内容 $('#div1').html ...