如何在C++中删除CString中的字符串

3
我要翻译的内容是:

我说的CString类型是微软的

我尝试了两种方法;

方法1)使用在msdn网站上找到的Remove()和Replace()函数,但Visual c++显示Error:argument of type "const char*" is incompatible with argument of type "wchar_t"

具体的代码如下:

#include <cstring>

CString sTmp= sBody.Mid(nOffset);
CString sFooter= L"https://" + sTmp.Mid(0, nEnd );
sFooter.Remove("amp;");

我不想一次只移除一个字符,因为这样会把其他的“a”也一起删除。

出于某些原因,当我将sFooter转换为std :: string时,字符串中会出现不应该存在的随机“amp;”...

我是这样将sFooter转换为std :: string的:

CT2CA p (sFooter);
string str (p);

方法2)将CString转换为std :: string,然后使用erase()和remove()来去掉“amp;”

#include <string>
#include <algorithm>

sFooter2.erase(std::remove(sFooter2.begin(), sFooter2.end(), "amp;"), sFooter2.end());

但是Visual Studio的输出显示如下:http://pastebin.com/s6tXQ48N
2个回答

5
尝试使用Replace函数。 sFooter.Replace(_T("&amp;"), _T(""));

1
错误信息非常明确。您需要使用宽字符串而不是常规字符串。
sFooter.Remove(L"amp;");

另外,根据文档Remove 方法只接受一个单一字符。你需要找到另一种方法来删除整个子字符串。


当我尝试这样做时,L下面有一条红线,显示“错误:类型为“const wchar_t *”的参数与类型为“wchar_t”的参数不兼容”。 - minusatwelfth

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接