CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
不看会后悔的Windows XP之经验谈 简单快捷DIY实用家庭影院
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  VC/MFC >  基础类

字符串查找的问题,整个单词的匹配

楼主microtan0902(microtan)2005-05-22 16:18:30 在 VC/MFC / 基础类 提问

有一段字符串"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

相关问题

  • 求助!!!关于查找匹配字符串!!
  • 如何在memo中查找到匹配的字符串,并选中他
  • 字符串匹配问题
  • 字符串匹配函数?
  • 字符串匹配问题
  • 救命!查找字符串!
  • 查找字符串!急!
  • 关于查找字符串
  • 字符串的查找????
  • 字符串查找替换

关键词

  • nstart
  • 单词
  • 匹配
  • 变红
  • richedit
  • strtext
  • getlength

得分解答快速导航

  • 帖主:microtan0902
  • zcjcom830
  • newcore

相关链接

  • Visual C++类图书
  • Visual C++类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo