strstr的使用案例

#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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值