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 ...
随机推荐
- [LeetCode 题解]: LetterCombinations
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- LDAP 相关链接
http://www.cnblogs.com/fish-li/archive/2012/05/07/2486840.html细说ASP.NET Windows身份认证 http://www.cnblo ...
- 【python】@property装饰器
Python内置的@property装饰器可以把类的方法伪装成属性调用的方式.也就是本来是Foo.func()的调用方法,变成Foo.func的方式.在很多场合下,这是一种非常有用的机制. class ...
- .NET Core + gRPC 实现数据串流 (Streaming)
引入 gRPC 是谷歌推出的一个高性能优秀的 RPC 框架,基于 HTTP/2 实现.并且该框架对 .NET Core 有着优秀的支持.最近在做一个项目正好用到了 gRPC,遇到了需要串流传输的问题. ...
- django drf 初探serializer
1.定义Model对应的serializer from rest_framework import serializers class GoodsSerializer(serializers.Seri ...
- 浅谈K8S cni和网络方案
此文已由作者黄扬授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 在早先的k8s版本中,kubelet代码里提供了networkPlugin,networkPlugin是一组接 ...
- RabbitMq初探——安装
rabbitmq Server安装 rabbitmq server安装很简单. 安装erlang环境 rpm -ihv erlang-18.1-1.el6.x86_64.rpm rpm -ihv ra ...
- 爬虫开发3.requests模块
requests模块 - 基于如下5点展开requests模块的学习 什么是requests模块 requests模块是python中原生的基于网络请求的模块,其主要作用是用来模拟浏览器发起请求.功能 ...
- 将yum源更换为阿里的源(脚本)
#!/bin/bash ######################################### #Function: update source #Usage: bash update_s ...
- leecode刷题(15)-- 验证回文字符串
leecode刷题(15)-- 验证回文字符串 验证回文字符串 给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写. 说明:本题中,我们将空字符串定义为有效的回文串. 示例 ...