strstr的使用案例

    xiaoxiao2022-06-22  20

    #include<stdio.h> #include<stdlib.h> #include <string.h> #include <ctype.h> /*去除字符串inStr中的空格*/ int trimSpace(char*inStr,char*out) { char *p = inStr; int ret = 0; int i; if (p==NULL) { ret = -1; return; } for (i=0;p[i]!='\0';i++) { if (p[i]!=' ') { *out++ = p[i]; } } *out='\0'; return ret; } /*在key-value字符串中根据key查找value 通过局部变量valuebuf返回*/ int getkeyByValue(char*keyvaluebuf,char*keybuf,char*valuebuf) { //查找key在不在 //查找= //去除空格 int ret = -1; char *p = keyvaluebuf; int i =0; char valuemidbuf[64]; int len= strlen(keyvaluebuf); if (keyvaluebuf==NULL||keybuf==NULL||valuebuf==NULL) { return -1; } p = strstr(p,keybuf); if (p == NULL) { printf("key not found\n"); ret = -1; return ret; } p = p + strlen(keybuf); p = strstr(p,"="); if (p == NULL) { printf("= not found\n"); ret = -1; return ret; } ret = trimSpace(p+1,valuebuf); if (ret == -1) { return ret; } return ret; } int main() { char* keyvaluebuf = "key2 = abcdef "; char* key = "key2"; char valuebuf[1024]; int ret = 0; ret = getkeyByValue(keyvaluebuf,key,valuebuf); if (ret!=0) { printf("error"); } printf("valuebuf:%s\n",valuebuf);//abcdef system("pause"); return 0; }
    转载请注明原文地址: https://ju.6miu.com/read-1122666.html

    最新回复(0)