c语言处理字符串

c语言处理字符串

在物联网项目开发过程中,经常使用http get(平台会提供相应的api接口)从服务器端获取一些数据,这些数据可能时Json格式的字符串,比较好处理,但是对于部分无规则的字符串,我们如何解析它呢?
首先介绍下ubuntu下man工具的使用
安装c语言库函数man手册,sudo apt-get install manpages-dev,man 3 c语言库函数 查询库函数用法

一.函数介绍

处理字符串,常用下列几个字符串处理函数
1.strstr
函数原型
char *strstr(const char *haystack, const char *needle);
man 3 strstr查看函数介绍

STRSTR(3)                                   Linux Programmer's Manual                                   STRSTR(3)

NAME
       strstr, strcasestr - locate a substring

SYNOPSIS
       #include <string.h>  //函数所属头文件

       char *strstr(const char *haystack, const char *needle);  //函数原型

       #define _GNU_SOURCE         /* See feature_test_macros(7) */

       #include <string.h>

       char *strcasestr(const char *haystack, const char *needle);  //同类函数

DESCRIPTION //函数介绍
       The strstr() function finds the first occurrence of the substring needle in the string haystack.  The ter‐
       minating null bytes ('\0') are not compared.

       The strcasestr() function is like strstr(), but ignores the case of both arguments.

RETURN VALUE //函数返回值
       These functions return a pointer to the beginning of the substring, or NULL if the substring is not found.



功能:函数用于判断字符串needle是否是haystack的子串。如果是,则该函数返回needle在haystack中首次出现的地址;否则,返回NULL。

2.strchr
函数原型
char *strchr(const char *s, int c);

STRCHR(3)                                                Linux Programmer's Manual                                               STRCHR(3)

NAME
       strchr, strrchr, strchrnul - locate character in string

SYNOPSIS
       #include <string.h>

       char *strchr(const char *s, int c);  //查找c在s字符串的第一次出现的位置,返回其地址

       char *strrchr(const char *s, int c);  //从字符串尾部查找c字符,返回其地址。

       #define _GNU_SOURCE         /* See feature_test_macros(7) */
       #include <string.h>

       char *strchrnul(const char *s, int c);

DESCRIPTION
       The strchr() function returns a pointer to the first occurrence of the character c in the string s.

       The strrchr() function returns a pointer to the last occurrence of the character 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 multibyte characters.

RETURN VALUE
       The strchr() and strrchr() functions return a pointer to the matched character or NULL if the character is not found.

       The strchrnul() function returns a pointer to the matched character, or a pointer  to  the  null  byte  at  the  end  of  s  (i.e.,
       s+strlen(s)) if the character is not found.


功能:在字符串s中查找第一个c字符出现的位置,返回其地址,否则返回null。

3.strtok
函数原型
char *strtok(char *str, const char *delim);



NAME
       strtok, strtok_r - extract tokens from strings

SYNOPSIS
       #include <string.h>

       char *strtok(char *str, const char *delim);

       char *strtok_r(char *str, const char *delim, char **saveptr);

   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

       strtok_r(): _SVID_SOURCE || _BSD_SOURCE || _POSIX_C_SOURCE >= 1 || _XOPEN_SOURCE || _POSIX_SOURCE

DESCRIPTION
       The strtok() function parses a string into a sequence of tokens.  On the first call to strtok() the string to be parsed should be specified in str.  In each subsequent call that should parse the
       same string, str should be NULL.

       The delim argument specifies a set of characters that delimit the tokens in the parsed string.  The caller may specify different strings in delim in successive calls that parse the same string.

       Each call to strtok() returns a pointer to a null-terminated string containing the next token.  This string does not include the delimiting character.  If no  more  tokens  are  found,  strtok()
       returns NULL.

       A  sequence  of two or more contiguous delimiter characters in the parsed string is considered to be a single delimiter.  Delimiter characters at the start or end of the string are ignored.  Put
       another way: the tokens returned by strtok() are always nonempty strings.

       The strtok_r() function is a reentrant version strtok().  The saveptr argument is a pointer to a char * variable that is used internally by strtok_r() in order to maintain context  between  suc‐
       cessive calls that parse the same string.

       On the first call to strtok_r(), str should point to the string to be parsed, and the value of saveptr is ignored.  In subsequent calls, str should be NULL, and saveptr should be unchanged since
       the previous call.

       Different strings may be parsed concurrently using sequences of calls to strtok_r() that specify different saveptr arguments.

RETURN VALUE
       The strtok() and strtok_r() functions return a pointer to the next token, or NULL if there are no more tokens.

功能:使用delim字符分割str字符串。其相关接口— strtok_r。
char *strtok_r(char *str, const char *delim, char **saveptr);
str 分解的字符串
delim 分割字符串,此处注意,传参字符串不是字符
saveptr 分割后剩余字符串

二.测试程序

下面编写一个测试程序,提取name:xian,sex:man,age:22,hobby:running各项参数。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


void main(void)
{
    char *string = "name:xian,sex:man,age:22,hobby:running"; //不是json格式,如何解析

    char temp[280] = ""; //备份string
    char * ret = NULL;//返回被字符分割出来的字符串
    char * retstr = NULL;//返回分割出的字符串
    char * outstring = NULL;//保存剩余值
    char * instring = NULL;

    strcpy(temp,string);
    instring = temp;
    //分割字符串,以,分割字符串
    while(NULL != (instring = strtok_r(instring,",",&outstring)))
    {
         printf("instring:%s\n",instring);
         printf("temp:%s\n",outstring);

         //查找某字符串,判断是否为某参数,如果时,打印参数值
         if((retstr = strstr(instring,"name")) != NULL)
         {
            //查找参数值
            ret = strchr(retstr,':');
            printf("name:%s\n",ret);
         }
         else if(NULL != (retstr = strstr(instring,"sex")))
         {
            //查找参数值
            ret = strchr(retstr,':');
            printf("sex:%s\n",ret);

         }
         else if(NULL != (retstr = strstr(instring,"age")))
         {
            //查找参数值
            ret = strchr(retstr,':');
            printf("age:%s\n",ret);

         }
         else if(NULL != (retstr = strstr(instring,"hobby")))
         {
            //查找参数值
            ret = strchr(retstr,':');
            printf("hobby:%s\n",ret);

         }
         instring = NULL;
    }
 return ;
}

注意:
1.第一次使用strtok_r之后,要把str置为NULL。
2.str不能指向常量指针,不然出现程序崩溃。
3.编写此类测试程序,容易发生段错误,这里提供一个调试定位段错误位置方法,
编译.c
gcc string.c -o string
执行
./string ------------->报错
在这里插入图片描述
调试
ltrace ./string
如果没有ltrace 则进行安装 sudo apt-get install ltrace
在这里插入图片描述
由此就能定位出出现段错误的方法,方便修改。

修改段错误位置后,下图为程序执行结果
在这里插入图片描述
有图可知,已经成功解析出字符串各项值。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值