如何在C++中摆脱CString中的字符串

min*_*fth 2 c++ string arguments cstring visual-c++

我所说的 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;");
Run Code Online (Sandbox Code Playgroud)

而且我不想一次删除一个字符,因为它会去掉其他的 'a'。

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

我正在将 sFooter 转换为 std::string ,如下所示:

CT2CA p (sFooter);
string str (p);
Run Code Online (Sandbox Code Playgroud)

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

#include <string>
#include <algorithm>

sFooter2.erase(std::remove(sFooter2.begin(), sFooter2.end(), "amp;"), sFooter2.end());
Run Code Online (Sandbox Code Playgroud)

但 Visual Studio 的输出显示了这一点:http : //pastebin.com/s6tXQ48N

Jos*_*son 5

尝试使用替换。

sFooter.Replace(_T("&amp;"), _T(""));