strchr字符查找函数族;strchrnul()

https://blog.csdn.net/jasonchen_gbd/article/details/80069134

Linux中的字符串和字节序列处理函数【Y】

字符串查找,有库函数,可以起到事半功倍的效果,不但是代码量减少,而且执行效率更高;

实际中使用到的字符串查找函数有:strchr(), strrchr(), strchrnul();

目录

函数描述

实例说明

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 }

执行结果:

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值