CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
【经验总结】不能实施并行处理的情况 浅谈并行编程中的任务分解模式
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  VC/MFC >  基础类

中文字符的处理

楼主Lansie(山河水)2000-07-28 14:46:00 在 VC/MFC / 基础类 提问

有一行包含字符的中文字符串,如下所示:  
  CString   strTmp("==============测试===================");  
  我要把'='去掉,用以下语句:  
  strTmp.Remove('=');  
  可其中中文就变成了?,这是怎么回事,因该如何操作才能正确的去除'='?  
  问题点数:30、回复次数:5Top

1 楼huntout(猎手)回复于 2000-07-28 16:17:00 得分 20

下面是Remove的源程序,問題就出在_tcsinc函數上,它的作用是指針向後位移一位,  
  但pstrDest指向漢字的第一個字節時,_tcsinc(pstrDest)會後移兩位。  
   
  int   CString::Remove(TCHAR   chRemove)  
  {  
  CopyBeforeWrite();  
   
  LPTSTR   pstrSource   =   m_pchData;  
  LPTSTR   pstrDest   =   m_pchData;  
  LPTSTR   pstrEnd   =   m_pchData   +   GetData()->nDataLength;  
   
  while   (pstrSource   <   pstrEnd)  
  {  
  if   (*pstrSource   !=   chRemove)  
  {  
  *pstrDest   =   *pstrSource;  
  pstrDest   =   _tcsinc(pstrDest);  
  }  
  pstrSource   =   _tcsinc(pstrSource);  
  }  
  *pstrDest   =   '\0';  
  int   nCount   =   pstrSource   -   pstrDest;  
  GetData()->nDataLength   -=   nCount;  
   
  return   nCount;  
  }  
   
  解決辦法是仿照自己重載一下Remove,考慮一下漢字。  
  Top

2 楼dyj1999(dyj1999)回复于 2000-07-28 16:23:00 得分 0

汉字不能用Remove,否则连汉字一起移去,若字母就没这个问题Top

3 楼softsprite(软件精灵)回复于 2000-07-28 16:30:00 得分 10

CString   strTmp   =   "=====中文=====";  
  int   i;  
  while   (   (i=strTmp.Find('='))   >=   0   )  
  {  
      strTmp.Delete(i);  
  }  
  Top

4 楼huntout(猎手)回复于 2000-07-28 16:51:00 得分 0

我以前遇到過這個問題,並曾經用過softsprite的方法,但當字符串很長時,極其耗時,不建議使用。可以用下面的函數,我已調試過︰  
   
  void   Remove(CString   &str,   char   chRemove)  
  {  
  int   nLen   =   str.GetLength();  
  char*   pstr   =   new   char[nLen   +   1];  
  strcpy(pstr,   (LPCTSTR)str);  
  char*   pstrSource   =   pstr;  
  char*   pstrDest   =   pstr;  
  char*   pstrEnd   =   pstr   +   nLen;  
   
  while   (pstrSource   <   pstrEnd)  
  {  
  if   (*pstrSource   !=   chRemove)  
  {  
  *pstrDest   =   *pstrSource;  
  if   (*pstrDest   <   0)  
  {  
  *(pstrDest+1)   =   *(pstrSource+1);  
  }  
  pstrDest   =   _tcsinc(pstrDest);  
  }  
  pstrSource   =   _tcsinc(pstrSource);  
  }  
  *pstrDest   =   '\0';  
  str   =   pstr;  
  delete[]   pstr;  
  }  
  Top

5 楼UserReg(用户注册)回复于 2000-07-28 17:15:00 得分 0

strTmp.Remove('=');  
  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  
  改为strTmp.Replace("=","");即可  
  Top

相关问题

  • 字符串中的中文字符长度处理
  • Socket传输中文字符处理!!高分求救
  • 处理字符串中中文和空格
  • 含有中文的字符串处理!急
  • 求中文字符串的处理问题?
  • 如何用C语言处理中文字符
  • 字符处理
  • 字符处理
  • 字符处理
  • 烦人:用Java操纵mySQL数据库时的中文字符处理。

关键词

  • 中文
  • pstrdest
  • pstrsource
  • strtmp
  • pstr
  • pchdata
  • nlen
  • remove
  • lptstr
  • cstring

得分解答快速导航

  • 帖主:Lansie
  • huntout
  • softsprite

相关链接

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

广告也精彩

反馈

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