c语言怎么找查字符串,C语言字符串查找函数

转自:blog.csdn.net/minpro

//此函数的功能是在一个长字符串中,查找子串

//仅保留,以便使用,请勿见笑!

/**************************************************************************

** 此函数的功能是在一个长字符串中,查找子串

** date : 2008-11-11

** env : HP-UX hp94 B.11.11 U 9000/800 4183772791 unlimited-user license

***************************************************************************/

/C语言实现

#include #include #include

#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{

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 #include #include #include

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

##################################################

*/

注:虽然很小,但是留下来,以后用的时候随便改改就行了。见笑了^_^

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值