C语言字符串查找函数
来源:互联网 发布:淘宝上最火的女装店铺 编辑:程序博客网 时间:2024/04/29 16:47
转自:blog.csdn.net/minpro
//此函数的功能是在一个长字符串中,查找子串
//仅保留,以便使用,请勿见笑!
/**************************************************************************
** 此函数的功能是在一个长字符串中,查找子串
** date : 2008-11-11
** env : HP-UX hp94 B.11.11 U 9000/800 4183772791 unlimited-user license
***************************************************************************/
/////C语言实现
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define PARALENGTH 50
typedef struct Content_Type_Para{
char paraName[PARALENGTH];
char paraValue[PARALENGTH];
}ContentTypePara_t;
void setContentType(const char *content)
{
//content参数形如"application/bear;params1=XXXYYY,params2=ZZZZSSS"
char contentTypeName[PARALENGTH];
char contentTypeValue[PARALENGTH];
memset(contentTypeName, 0, PARALENGTH);
memset(contentTypeValue, 0, PARALENGTH);
int ContentTypeParasNumber = 0; //参数个数
ContentTypePara_t contentTypeParas[5]; //参数数组
for(int k=0; k< 5; k++)
{
memset(contentTypeParas[k].paraName, 0, PARALENGTH);
memset(contentTypeParas[k].paraValue, 0, PARALENGTH);
}
//在临时空间里操作字符串
char *p;
char temp[1024];
memset(temp, 0, sizeof(temp));
strncpy(temp, content, strlen(content));
p = temp;
//临时子串
char *tp;
char subtemp[PARALENGTH];
memset(subtemp, 0, PARALENGTH);
tp = subtemp;
char flag; //用来标识字符串的分割点
//字符串查找
for(; *p != '/0'; p++)
{
if((*p != '/')&&(*p != ';')&&(*p != '=')&&(*p != ',')) //连续的字符串,复制
{
*tp = *p;
tp++;
}
else if(*p =='/') //'/'之前的字符串是contentTypeName
{
strncpy(contentTypeName, subtemp, strlen(subtemp));
memset(subtemp, 0, strlen(subtemp)); //清空临时子串
tp = subtemp;
flag = '/'; //标识一个子串的结束
}
else if(*p == ';') //如果参数之间使用',',那么这个头字段只有一个';'
{
if(flag == '/') //';'之前的flag一定是'/'
{
strncpy(contentTypeValue, subtemp, strlen(subtemp));
memset(subtemp, 0, strlen(subtemp));
tp = subtemp;
flag = ';';
}
}
else if(*p == '=') //参数的赋值使用'='
{
if((flag == ';') || (flag == ','))
{
strncpy(contentTypeParas[ContentTypeParasNumber].paraName, subtemp, strlen(subtemp));
memset(subtemp, 0, strlen(subtemp));
tp = subtemp;
flag = '=';
}
}
else if(*p == ',')
{
if(flag == '=')
{
strncpy(contentTypeParas[ContentTypeParasNumber].paraValue, subtemp, strlen(subtemp));
memset(subtemp, 0, strlen(subtemp));
tp = subtemp;
flag = ',';
ContentTypeParasNumber++;
}
}
}
//最后一个子串
if(flag == '=')
{
strncpy(contentTypeParas[ContentTypeParasNumber].paraValue, subtemp, strlen(subtemp));
memset(subtemp, 0, strlen(subtemp));
tp = subtemp;
ContentTypeParasNumber++;
}
else if(flag = '/') //没有参数的情况
{
strncpy(contentTypeValue, subtemp, strlen(subtemp));
memset(subtemp, 0, strlen(subtemp));
tp = subtemp;
}
printf("|*|*|***********************************************/n");
printf("|*|*|contentTypeName : %s /n", contentTypeName);
printf("|*|*|contentTypeValue : %s /n", contentTypeValue);
for(int i = 0; i<ContentTypeParasNumber; i++)百思博客3wd-|5CYa&|b
{
printf("|*|*|i : %d , para name : %s /n", i, contentTypeParas[i].paraName);
printf("|*|*|i : %d , para value : %s /n", i, contentTypeParas[i].paraValue);
}
printf("|*|*|***********************************************/n");
}
void main()
{
setContentType("application/bear;params1=XXXYYY,params2=ZZZZSSS");
}
/*
运行结果:
|*|*|***********************************************
|*|*|contentTypeName : application
|*|*|contentTypeValue : bear
|*|*|i : 0 , para name : params1
|*|*|i : 0 , para value : XXXYYY
|*|*|i : 1 , para name : params2
|*|*|i : 1 , para value : ZZZZSSS
|*|*|***********************************************
*/
//////C++语言实现
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <string>
bool setContentType(string content)
{
//application/bear;boundary=stringXXX
int contentTypeParaNum = 0;
string contentTypeName;
string contentTypeValue_s;
string contentTypeParaName_s;
string contentTypeParaValue_s;
string tempStr = content;
if(tempStr.size() == 0)
{
printf("content-type string is NULL !");
return false;
}
string::size_type pos = tempStr.find_first_of("/");
contentTypeName = tempStr.substr(0, pos);
tempStr.erase(0, pos+1);
pos = tempStr.find_first_of(";");
contentTypeValue_s = tempStr.substr(0, pos);
tempStr.erase(0, pos+1);
pos = tempStr.find_first_of("=");
contentTypeParaName_s = tempStr.substr(0, pos);
tempStr.erase(0, pos+1);
contentTypeParaValue_s = tempStr;
contentTypeParaNum++;
printf("##################################################/n");
printf("###contentTypeName : %s /n", contentTypeName.c_str());
printf("###contentTypeValue_s : %s /n", contentTypeValue_s.c_str());
printf("###contentType para number : %d /n", contentTypeParaNum);
printf("###contentTypeParaName_s : %s /n", contentTypeParaName_s.c_str());
printf("###contentTypeParaValue_s : %s /n", contentTypeParaValue_s.c_str());
printf("##################################################/n");
return true;
}
int main()
{
string content("application/bear;boundary=stringXXX");
setContentType(content);
return 0;
}
/*
##################################################
###contentTypeName : application
###contentTypeValue_s : bear
###contentType para number : 1
###contentTypeParaName_s : boundary
###contentTypeParaValue_s : string
##################################################
*/
注:虽然很小,但是留下来,以后用的时候随便改改就行了。见笑了^_^
- C语言字符串查找函数
- C语言的字符串查找函数
- 【C语言】查找字符串
- C语言查找字符串
- C 字符串查找函数
- C 字符串查找函数
- C语言与字符串相关的查找函数
- C语言——字符串查找字符的两个函数
- C语言字符串查找位置
- C语言字符串查找替换
- C/C++字符串查找函数
- C/C++字符串查找函数
- C/C++字符串查找函数
- C/C++字符串查找函数
- C/C++字符串查找函数
- C/C++字符串查找函数
- C/C++字符串查找函数
- C/C++字符串查找函数
- C++图像缩放
- 使用oprofile分析性能瓶颈
- 使用ifstream和getline读取文件内容
- PyQt4实现USB设备插入到弹出的检测(Windows)
- 关于new创建对象的弱智问题
- C语言字符串查找函数
- java 参数是按值传递还是按址传递
- FFMPEG的详细资料可以在它的官方网站上找到
- C++的类型转换符:static_cast、dynamic_cast、reinterpret_cast和const_cast
- RSpec + Spork + Autotest 给Rails 3添加快速自动化测试
- Linux守护进程
- 如何截取UNIX终端的全部输出信息
- C++类中拷贝构造函数详解
- Unicode与C程序——《windows程序设计》