字符串查找的问题,整个单词的匹配
有一段字符串"what is the best book? there is only one."我想在其中查找the这个单词(注意是单词)并将其变红.
nStart = m_csRichEdit.Find("the",nStart+3);
if(nStart >= 0)
{
m_RichEdit.SetFocus();
m_RichEdit.SetSel((long)nStart, (long)nStart + sFindWord[i].GetLength());
m_RichEdit.SetSelectionCharFormat(cf);
m_RichEdit.SetSel(0,0);
}
我这样写代码发现把there中的the也变红了.
请问有什么方法能对整个单词进行匹配,就是能使is后面的the变红,而there中的the不做任何变化?
(请不用匹配there,因为该串中还有很多含有the的单词).
问题点数:20、回复次数:3Top
1 楼zcjcom830()回复于 2005-05-22 17:00:59 得分 2
查"#the#" 其中'#'代表着各种空格、Tab、回车等字符Top
2 楼newcore(to be or not to be, it's a question.)回复于 2005-05-22 17:10:46 得分 18
the出现不外乎两种情况:
1 string中:"the "
2 string末尾:"the"
必须都找到:
下面的可用:
CString strText = "what is the best book? the is only one the";
CString strFind = "the";
int nStart = 0;
while(nStart>=0)
{
nStart = strText.Find (strFind, nStart);
if(nStart>0)
{
if( (nStart+3)>=strText.GetLength() )
{
cout<<"string " <<"the "<<"begin at : "<<nStart<<endl;
break ;
}
if(strText[nStart+3]==' ')
{
cout<<"string " <<"the "<<"begin at : "<<nStart<<endl;
nStart+=3;
}
}
}
Top
3 楼newcore(to be or not to be, it's a question.)回复于 2005-05-22 17:14:17 得分 0
不好意思,有点错误,试试下面的,不行包换:
CString strText = "what is thee best book? the is only one the";
CString strFind = "the";
int nStart = 0;
while(nStart>=0) // &&nStart<strText.GetLength ())
{
nStart = strText.Find (strFind, nStart);
if(nStart>0)
{
if( (nStart+3)>=strText.GetLength() )
{
cout<<"string " <<"the "<<"begin at : "<<nStart<<endl;
break ;
}
else
{
if(strText[nStart+3]==' ')
{
cout<<"string " <<"the "<<"begin at : "<<nStart<<endl;
}
nStart+=3;
}
}
}Top




