https://blog.csdn.net/jasonchen_gbd/article/details/80069134
Linux中的字符串和字节序列处理函数【Y】
字符串查找,有库函数,可以起到事半功倍的效果,不但是代码量减少,而且执行效率更高;
实际中使用到的字符串查找函数有:strchr(), strrchr(), strchrnul();
目录
函数描述
NAME
strchr, strrchr, strchrnul - locate character in string
SYNOPSIS
#include <string.h>
char *strchr(const char *s, int c);
char *strrchr(const char *s, int c);
#define _GNU_SOURCE // 特别注意,必须是这种格式:#define + <string.h>,否则不能编译成功
#include <string.h>
char *strchrnul(const char *s, int c);
DESCRIPTION
The strchr() function returns a pointer to the first occurrence of the charac-ter c in the string s.
The strrchr() function returns a pointer to the last occurrence of the charac-ter c in the string s.
The strchrnul() function is like strchr() except that if c is not found in s, then it returns a pointer to the null byte at the end of s, rather than NULL.
Here "character" means "byte"; these functions do not work with wide or multi-byte characters.
RETURN VALUE
The strchr() and strrchr() functions return a pointer to the matched character or NULL if the character is not found.
VERSIONS
strchrnul() first appeared in glibc in version 2.1.1.
CONFORMING TO
strchr() and strrchr() are in SVr4, 4.3BSD, C89, C99. strchrnul() is a GNU extension.
实例说明
-
strchr()
1 #include <string.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4
5 int main(viod)
6 {
7 char *str = {"0123456789asdfghkjkl;0123456789"};
8 char *tmp = NULL;
9
10 tmp = strchr(str, '5');
11 printf("find ‘5’.\n");
12 printf("str = %s.\n", str);
13 printf("tmp = %s.\n", tmp);
14
15 tmp = strchr(str, '3');
16 printf("find ‘3’.\n");
17 printf("str = %s.\n", str);
18 printf("tmp = %s.\n", tmp);
19
20 tmp = strchr(str, ';');
21 printf("find ‘;’.\n");
22 printf("str = %s.\n", str);
23 printf("tmp = %s.\n", tmp);
24
25
26 system("pause");
27 return 0;
28 }
执行结果:
-
strrchr()
1 #include <string.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4
5 int main(viod)
6 {
7 char *str = {"0123456789asdfghkjkl;0123456789"};
8 char *tmp = NULL;
9
10 tmp = strrchr(str, '5');
11 printf("find ‘5’.\n");
12 printf("str = %s.\n", str);
13 printf("tmp = %s.\n", tmp);
14
15 tmp = strrchr(str, '3');
16 printf("find ‘3’.\n");
17 printf("str = %s.\n", str);
18 printf("tmp = %s.\n", tmp);
19
20 tmp = strrchr(str, ';');
21 printf("find ‘;’.\n");
22 printf("str = %s.\n", str);
23 printf("tmp = %s.\n", tmp);
24
25 system("pause");
26 return 0;
27 }
28
执行结果:
strchrnul()
一定要注意,#define _GNU_SOURCE 放到最前面
1 #define _GNU_SOURCE
2
3 #include <string.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6
7
8 int main(viod)
9 {
10 char *str = {"0123456789asdfghkjkl;0123456789"};
11 char *tmp = NULL;
12
13 tmp = strchrnul(str, '5');
14 printf("find ‘5’.\n");
15 printf("str = %s.\n", str);
16 printf("tmp = %s.\n", tmp);
17
18 tmp = strchrnul(str, 'Y');
19 printf("find ‘Y’.\n");
20 printf("str = %s.\n", str);
21 printf("tmp = %s.\n", tmp);
22
23 tmp = strchrnul(str, '?');
24 printf("find ‘?’.\n");
25 printf("str = %s.\n", str);
26 printf("tmp = %s.\n", tmp);
27
28
29 system("pause");
30 return 0;
31 }
执行结果: