c总结6 --- 在字符串键值对中提取Value(指针学习5)
2015-10-19 10:31
375 查看
我们在开发的时候,还有可能会在一段含有键值对中的字符串中进行查找键对应得值,比如:姓名=张三 等等。这样我们就需要借助今天的总结进行操作。
我们需要判断要查找的键是否存在,只有存在的情况下,后面的操作才有意义。
找到键后要 = 字符
然后就可以获取到,我们需要的键值了
备注:考虑到要操作的字符串的含有空白字符的情况,所以我们需要借助上篇总结。
直接上代码:
我们需要判断要查找的键是否存在,只有存在的情况下,后面的操作才有意义。
找到键后要 = 字符
然后就可以获取到,我们需要的键值了
备注:考虑到要操作的字符串的含有空白字符的情况,所以我们需要借助上篇总结。
直接上代码:
/************************************************************************/ /* 函数名称:myStrTrim /* 函数功能: 将前后 含有空白字符的字符串 提出出来 /* 参数1: 前后含有空白字符的字符串 /* 参数2: 去除空白字符的字符串 /* 返回值: 非0代表失败 /* 说明: #include <ctype.h> int isspace( int ch ); 如果参数是空格类字符(即:单空格,制表符,垂直制表符,满页符,回车符,新行符), 函数返回非零值,否则返回零值。 /************************************************************************/ int myStrTrim1(char * p, char * buf) { int ret = 0; int ncount = 0; //定义两个下标 一个从0开始 一个从后面开始 int i, j; if (p == NULL || buf == NULL) { ret = -1; return ret; } //第一个下标 i = 0; //最后一个下标, 是 \0前面的字符的下标 j = strlen(p) - 1; //如果是空白字符 并且 不为'\0' 开始下标自增 while (isspace(p[i]) && p[i] != '\0') { i++; } //如果是空白字符 并且j不是0 末尾下标自减 while (isspace(p[j]) && j>0) { j--; } //用后面的下标 减去 前面的下标 获取到中间的字符数量 //因为多减一个 所以再加一 //这就获取到了 有效字符串 不含有\0 的字符个数 ncount = j - i + 1; //从 p+i的位置 拷贝 ncount个字符到 buf中去 strncpy_s(buf, ncount+1, p + i, ncount); //最后一个字符添加上 \0 buf[ncount] = '\0'; return ret; } /************************************************************************/ /* 函数名称:getKeyByValue /* 函数功能:在键值对的字符串中 查找键 所对应的值 /* 参数1: 要查找的字符串 /* 参数2: 要查找的键 /* 参数3: 键所对应的值 /* 返回值: 0表示成功 非0表示失败 /************************************************************************/ int getKeyByValue(char *pKeyValue, char *pKey, char *pValue) { char rv = 0; char * p = NULL; if (pKeyValue == NULL || pKey == NULL || pValue ==NULL) { rv = -1; printf("func getKeyByValue err:%d \n", rv); return rv; } //在pKeyValue中查是否存在 关键字 pKey p = strstr(pKeyValue, pKey); //如果找到则p指向pKey所在的位置 if (p == NULL) { //不存在 pKey rv = -1; printf("func getKeyByValue err:%d \n", rv); return rv; } //为下一次检索 = 做准备 p = p + strlen(pKey); //有没有= p = strstr(p, "="); if (p==NULL) { //不存在 = rv = -2; printf("func getKeyByValue err:%d \n", rv); return rv; } //为下一下检索 pValue 做准备 p = p + 1; //提取pKey对用的pValue值 rv = myStrTrim1(p, pValue); if (rv != 0) { printf("func myStrTrim err:%d \n", rv); return rv; } return 0; } int _tmain(int argc, _TCHAR* argv[]) { char KeyValue[] = "key61=value1"; char Key[] = "key1"; char Value[1024] = { 0 }; int ret = getKeyByValue(KeyValue, Key, Value); if (ret != 0) { printf("func:getKeyByValue() err:%d \n", ret); } else { printf("Value:%s \n", Value); } // char p[] = " adc "; // char buf[120] = { 0 }; // int a = myStrTrim( p,buf); // printf("%d %s \n", a, buf); system("pause"); return 0; }
相关文章推荐
- 洛谷1373 小a和uim之大逃离
- A B两个相同长度整数数组,判断相同下标对应值是否相等,true或者false 存在布尔数组C中
- UI15_XML, JSON解析
- String、StringBuffer和StringBuilder的区别
- 关于本科毕业论文《Laguerre小波在数值积分与微分方程数值解中的应用》存在的问题与小结
- Unique Binary Search Trees II
- easyui-combo个人实例
- WWDC2015 - Interface Builder技巧(IB 技巧)
- 长时间停留在calculating requirements and dependencies
- Extjs form.submit()提交与Ext.Ajax.request的区别
- 仿QQUI实现(1)
- Single value range only allowed in SystemVerilog
- stack和queue
- UIScrollView
- Easyui Datagrid使用小例
- Git GUI的使用
- iOS UITableView-FDTemplateLayoutCell框架 cell重叠 高度返回0.5问题解决
- Android开发系列(三) 跟随移动userguide
- Unique Binary Search Trees
- IOS基础UI之(八)UIScrollView