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 <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
##################################################
*/
注:雖然很小,但是留下來,以後用的時候隨便改改就行了。見笑了^_^