strtok使用代码参考

注:strtok可以指定多个断开符,如:char seps[]   = " ,\t\n";

strtok指定的字符串可以为常量,如 string::c_str()

string string1 = "A string\tof ,,tokens\nand some  more tokens";
char seps[]   = " ,\t\n";
char *token;


int main( void )
{
printf( "Tokens:\n" );


// Establish string and get the first token:
token = strtok( (char*)string1.c_str(), seps ); // C4996
// Note: strtok is deprecated; consider using strtok_s instead
while( token != NULL )
{
// While there are tokens in "string"
printf( " %s\n", token );


// Get next token: 
token = strtok( NULL, seps ); // C4996
}
int i = 0 ;
std::cin.get() ;
}


这个函数的功能是将:os=**,res=**,lan=**,ua=**拆分成各个字符串

void CTechFilter::GetDebugFilterInfo(const char* caryTaskData, string& strUserAgentData, string& strLanData, string& strResData, string& strUrlData)

{
int nLen = strlen(caryTaskData) ;
char* caryTaskDataTemp = new char[nLen + 1] ;
strcpy(caryTaskDataTemp, caryTaskData) ;
caryTaskDataTemp[nLen] = 0 ;

char* tokenPtr = strtok(caryTaskDataTemp, ",") ;
while (NULL != tokenPtr)
{
string strTemp(tokenPtr) ;
int nPos ;
if (string::npos != (nPos = strTemp.find("ua")))
{
strUserAgentData = strTemp.substr(3) ;
}
else if (string::npos != (nPos = strTemp.find("lan")))
{
strLanData = strTemp.substr(4) ;
}
else if (string::npos != (nPos = strTemp.find("res")))
{
strResData = strTemp.substr(4) ;
}
else if (string::npos != (nPos = strTemp.find("url")))
{
strUrlData = strTemp.substr(4) ;
}
else
{
CHDTLog::Instance()->WriteLogUnknow("CTechFilter::GetDebugFilterInfo", "输入的过滤参数格式有问题", "Tech_Filter.dll") ;
throw runtime_error("输入的过滤参数格式有问题") ;
}
tokenPtr = strtok(NULL, ",") ;
}

delete caryTaskDataTemp ;
}

你可能感兴趣的:(String,null,url,delete,token,deprecated)