(standard c libraries translation )index

来源:互联网 发布:淘宝手机端模板编辑 编辑:程序博客网 时间:2024/04/29 08:05
index, rindex - locate character in string
index,rindex-在字符串中定位字符

所需头文件
#include <strings.h>
char *index(const char *s, int c);
char *rindex(const char *s, int c);

The index() function returns a pointer to the first occurrence of the character c in the string s.
The rindex() function returns a pointer to the last occurrence of the character c in the string s.
The terminating null byte ('\0') is considered to be a part of the strings.
index函数返回一个指向字符串s中字符c第一次出现的位置
rindex函数返回一个指向字符串s中字符c最后一次出现的位置
字符串必须包含结束字符\0

返回值
The index() and rindex() functions return a pointer to the matched character or NULL if the character is not found.
index()和rindex()函数返回一个指向匹配字符的指针,如果没有找到则返回NULL

marked  as  LEGACY  in  POSIX.1-2001.   POSIX.1-2008 removes the specifications of index() and rindex(), recommending strchr(3) and strrchr(3) instead.
在POSIX.1-2001中被标记,在POSIX.1-2008中删掉了index()和rindex的定义,推荐使用strchr和strrchr作为替代


testcase如下:

#include <stdio.h>#include <string.h>int main(void){char *s = "abcddcba";char *tmp = index(s, 'b');printf("tmp = %s\n", tmp);tmp = rindex(s, 'b');printf("tmp = %s\n", tmp);return 0;}

运行结果如下:

cheny.le@cheny-ThinkPad-T420:~/cheny/testCode$ ./a.out
tmp = bcddcba
tmp = ba

0 0