c语言解析字符,c语言解析字符串

发布时间:2022-11-26

本文目录一览:

  1. C语言如何很好的解析字符串
  2. c语言解析字符串 ,大家请进
  3. c语言,解析字符串

C语言如何很好的解析字符串

解析字符串 可以灵活使用 memcmp strcmp strncmp等比较函数 另外 还有不区分大小的的比较函数,不同平台名字不同,非通用。 自己常用的一些比较 解析功能可以封装为自定义函数,或者宏函数。

c语言解析字符串 ,大家请进

不知道你要解析的字符串是否都包含这些项目并且位置固定,所以写了个通用的函数来取值。函数的3个参数分别是要解析的字符串,要取值的项目名,用来保存值的字符串,返回值表示是否成功。

#include stdio.h
#include string.h
int GetVal(const char *str, const char *name, char *val)
{
    if(str = strstr(str, name))
        for(str += strlen(name) + 1; *str && *str != ';'; *val++ = *str++)
            ;
    *val = '\0';
    return str != NULL;
}
int main()
{
    char str[] = "Fee=500;MonthType=1;ChargeMode=1;IfAutoOrder=月份;Fee1=46;feeType=2;billingUnit=0;maxfee=0";
    char Fee[10], MonthType[10], ChargeMode[10], IfAutoOrder[10];
    char Fee1[10], feeType[10], billingUnit[10], maxfee[10];
    if(GetVal(str, "Fee", Fee))
        printf("Fee=%s\n", Fee);
    if(GetVal(str, "MonthType", MonthType))
        printf("MonthType=%s\n", MonthType);
    if(GetVal(str, "ChargeMode", ChargeMode))
        printf("ChargeMode=%s\n", ChargeMode);
    if(GetVal(str, "IfAutoOrder", IfAutoOrder))
        printf("IfAutoOrder=%s\n", IfAutoOrder);
    if(GetVal(str, "Fee1", Fee1))
        printf("Fee1=%s\n", Fee1);
    if(GetVal(str, "feeType", feeType))
        printf("feeType=%s\n", feeType);
    if(GetVal(str, "billingUnit", billingUnit))
        printf("billingUnit=%s\n", billingUnit);
    if(GetVal(str, "maxfee", maxfee))
        printf("maxfee=%s\n", maxfee);
    return 0;
}

c语言,解析字符串

Result:

ss[0]=home
ss[1]=ubuntu
ss[2]=test
ss[3]=homework