中文字符的处理

Lansie 2000-07-28 02:46:00
有一行包含字符的中文字符串,如下所示:
CString strTmp("==============测试===================");
我要把'='去掉,用以下语句:
strTmp.Remove('=');
可其中中文就变成了?,这是怎么回事,因该如何操作才能正确的去除'='?
...全文
194 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
UserReg 2000-07-28
  • 打赏
  • 举报
回复
strTmp.Remove('=');
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
改为strTmp.Replace("=","");即可
huntout 2000-07-28
  • 打赏
  • 举报
回复
我以前遇到過這個問題,並曾經用過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;
}
U皮特U 2000-07-28
  • 打赏
  • 举报
回复
CString strTmp = "=====中文=====";
int i;
while ( (i=strTmp.Find('=')) >= 0 )
{
strTmp.Delete(i);
}
dyj1999 2000-07-28
  • 打赏
  • 举报
回复
汉字不能用Remove,否则连汉字一起移去,若字母就没这个问题
huntout 2000-07-28
  • 打赏
  • 举报
回复
下面是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,考慮一下漢字。

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧