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 ...
随机推荐
- 搭建IntelliJ IDEA授权服务
废话不多,直接开始 下载IntelliJ IDEA授权服务软件v1.4 密码:mu3t 下载IntelliJ IDEA授权服务软件v1.3 密码:1odn 选择你自己服务器的版本,我这里选择Intel ...
- velocity的日志解决问题
问题描述:velocity使用时,添加了一个非自己想要的日志文件,因此在velocity的配置中需要添加logger. 解决: velocity.properties添加log配置: runtime. ...
- [LINK]List of .NET Dependency Injection Containers (IOC)
http://www.hanselman.com/blog/ListOfNETDependencyInjectionContainersIOC.aspx I'm trying to expand my ...
- [HAOI2008] 排名系统
题目链接:戳我 要注意因为数可能会对应很多人,但是输出的时候要按照添加的顺序输出.所以我们不能将相同值的节点合并,用set维护.就算值相同也只能新开节点. 然后就没有什么了...懒得写哈希表..直接上 ...
- linux 安全配置随笔
1. 禁止Ctrl+Alt+Del直接重启服务器 /bin/mv /etc/init/control-alt-delete.conf /etc/init/control-alt-delete.conf ...
- centos有两个版本的apache
service服务链接更新 cp /usr/local/apache/bin/apachectl /etc/rc.d/init.d/httpd
- C++小总结
1.C与C++的简单区别 1.建立的文件类型不一样,C语言是.c,C++是.cpp 2.引入的头文件不一样 3.C++有命名空间 4.输入输出语句不一样 5.C语言不允许重载,C++可以重载 6.自定 ...
- Oracle数据库中日期/数字和字符之间的转换和计算
--查出当前系统时间 select SYSDATE from table; --格式转换 -- TO_CHAR 把日期或数字转换为字符串 -- TO_CHAR(number, '格式') -- TO_ ...
- UITextInputMode
An instance of the UITextInputMode class represents the current text-input mode. You can use this ob ...
- 查看Xcode配置文件
终端命令 open ~/Library/MobileDevice/Provisioning\ Profiles/ 除去多余的配置文件 Xcode -> Build Settings -> ...