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

时间:2012-08-01 02:13:28

标签: c++ string visual-c++ arguments cstring

我所说的CString类型是microsoft's

我尝试了两种方法;

方法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;");

而且我不想一次删除一个角色,因为它会摆脱其他角色。

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

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

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

方法2)将CString转换为std :: string,然后使用erase()和remove()来摆脱&#34; amp;&#34; s

#include <string>
#include <algorithm>

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

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

2 个答案:

答案 0 :(得分:3)

尝试使用替换。

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

答案 1 :(得分:0)

错误信息非常清楚。而不是使用常规字符串,您需要使用宽字符串。

sFooter.Remove(L"amp;");

同样according to the documentation Remove方法只接受一个字符。您需要找到另一种方法来删除整个子字符串。