//把strText 2个括号里面的类容显示出来
CString strText="VoSKY9000 Message 1: (slksm2001) is busy, please call user (slksm2002)";1。
CString tmp;
int pos1, pos2;
if ((pos1 = strText.Find("(",0)) != -1)
{
pos2 = strText.Find(")",pos1);
if (pos2 != -1)
{
tmp = strText.Mid(pos1+1,pos2-pos1-1);
AfxMessageBox(tmp);//显示出来
if ((pos1=strText.Find("(",pos2)) != -1 )
{
pos2 = strText.Find(")",pos1);
if (pos2 != -1)
{
tmp = strText.Mid(pos1+1,pos2-pos1-1);
AfxMessageBox(tmp);//显示出来
}
}
}
}2。
#ifdef tan
char *pstr;
char *token,seps[]=" ";
pstr = strText.GetBuffer(256);
token = strtok( pstr, seps );
int i=0;
while( token != NULL )
{
i++;
if((i==4) || (i==10))
{
char buff[255],*p=NULL,*q=NULL;
memset(buff,0,20);
p=strstr(token,"(");
p+=1;
q=strstr(p,")");
strncpy(buff,p,q-p);
AfxMessageBox(buff);//打印出来
}
token = strtok( NULL, seps );
}
#endif
CString strText="VoSKY9000 Message 1: (slksm2001) is busy, please call user (slksm2002)";1。
CString tmp;
int pos1, pos2;
if ((pos1 = strText.Find("(",0)) != -1)
{
pos2 = strText.Find(")",pos1);
if (pos2 != -1)
{
tmp = strText.Mid(pos1+1,pos2-pos1-1);
AfxMessageBox(tmp);//显示出来
if ((pos1=strText.Find("(",pos2)) != -1 )
{
pos2 = strText.Find(")",pos1);
if (pos2 != -1)
{
tmp = strText.Mid(pos1+1,pos2-pos1-1);
AfxMessageBox(tmp);//显示出来
}
}
}
}2。
#ifdef tan
char *pstr;
char *token,seps[]=" ";
pstr = strText.GetBuffer(256);
token = strtok( pstr, seps );
int i=0;
while( token != NULL )
{
i++;
if((i==4) || (i==10))
{
char buff[255],*p=NULL,*q=NULL;
memset(buff,0,20);
p=strstr(token,"(");
p+=1;
q=strstr(p,")");
strncpy(buff,p,q-p);
AfxMessageBox(buff);//打印出来
}
token = strtok( NULL, seps );
}
#endif
=======不嵌套的情况
CString strText="VoSKY9000 Message 1: (slksm2001) is busy, please call user (slksm2002)";
CString resToken;
int curPos= 0; resToken= strText.Tokenize("()",curPos);
while (resToken != "")
{
if((strText.GetLength() >= curPos) && (strText[curPos-1] == ')') )
{
std::cout << resToken << endl;
}
resToken= strText.Tokenize("()",curPos);
};
if(!pi) return;
char* p2 = strchr(p1, ')');
if(!p2) return;
CString strExt(p1, p2-p1);取第二个同上.
2 可以检查匹配情况
大家run下:#define TOKEN 12 //处理嵌套层数,可选...CString strText = "Vo(SKY9000) Message 1:(slksm(2001) is (bu)sy,pl)ease call user (slksm2002)";
CString strToken[TOKEN] = {""};int StrHandle(const CString& strText)
{
int LeftBracket[TOKEN] = {0};
int RightBracket[TOKEN] = {0};
LPCTSTR pTrav = (LPCTSTR)strText ;
LPCTSTR pPrev = pTrav;
printf("%s\n", pTrav);
int nLeft = 0, nRight = 0, nStrLength = strText.GetLength();
int nLeftNest = 0, nRightNest = 0;
// char cLast ;//= 0;
while(pTrav-pPrev<nStrLength)
{
while(pTrav!=NULL&&*pTrav!='('&&*pTrav!=')')
pTrav++;
if(*pTrav=='(')
{
LeftBracket[nLeft++] = pTrav++ - pPrev;
nLeftNest++;
printf("%d\n" , LeftBracket[nLeft-1]);
}
if(*pTrav==')')
{
nRight++;
nRightNest = nLeftNest - 1;
while(RightBracket[nRightNest]!=0)
nRightNest--;
RightBracket[nRightNest] = pTrav++ - pPrev;
printf("%d\n" , RightBracket[nRight-1]);
}
if(pTrav=="")
break;
}
if(nLeft!=nRight)
{
printf("error : black nest\n");
return -1;
}
int nLength = 0;
nLeftNest--;
for(int i=0; i<=nLeftNest; i++)
{
nLength = RightBracket[nLeftNest-i]-LeftBracket[nLeftNest-i]-1;
memcpy(strToken[i].GetBuffer(nLength) ,
pPrev + LeftBracket[nLeftNest-i]+1,
nLength);
printf("%s\n", (LPCTSTR)strToken[i]);
}
return 1;
}