minusatwelfth
minusatwelfth

Reputation: 191

How to get rid of string of characters in CString in C++

The CString type I am talking about is microsoft's.

I've attempted two methods;

Method 1) using the Remove() and Replace() function found on msdn website, and Visual c++ says Error:argument of type "const char*" is incompatible with argument of type "wchar_t"

Specifically my code is:

#include <cstring>

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

And I don't want to remove a single character at a time because it will get rid of other 'a's.

For some reason, when I convert sFooter to a std::string, random "amp;"s appear in the string that shouldn't be there...

I am converting sFooter to a std::string like this:

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

Method 2) Converting the CString to std::string, then using erase() and remove() to get rid of the "amp;"s

#include <string>
#include <algorithm>

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

But Visual studio's output shows this: http://pastebin.com/s6tXQ48N

Upvotes: 3

Views: 10063

Answers (2)

Joseph Willcoxson
Joseph Willcoxson

Reputation: 6040

Try using Replace.

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

Upvotes: 5

Mark Ransom
Mark Ransom

Reputation: 308206

The error message is pretty clear. Instead of using regular strings you need to use wide strings.

sFooter.Remove(L"amp;");

Also according to the documentation the Remove method only accepts a single character. You'll need to find another way to remove an entire substring.

Upvotes: 1

Related Questions