CString replace w/o using str.Replace(",,",",");


I need to replace duplicate commas in a CString.  I cannot get the str.Replace(",,",",");   to work.

I get the following compile error....
D:\EDIthingy\JobReconciliation.cpp(406) : error C2039: 'Replace' : is not a member of 'CString'
Error executing cl.exe.

CSXJobReconciliation.exe - 1 error(s), 0 warning(s)

Is this simply a missing include file or is there an alternative method for accomplishing this type of task.

thanks
knel
LVL 3
knel1234Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

chensuCommented:
What version of Visual C++ are you using? CString::Replace is a new member function since version 6.0 if I remember correctly.
0
mikeblasCommented:


Indeed, you should have CString::Replace(). It's a real function, and it's in there. I wrote it myself.

But if you don't have a new enough version of VC++, you can use something like this:

void RipDoubleCommas(CString& refStr)
{
   LPTSTR pstrSource = refStr.GetBuffer(0);
   LPTSTR pstrDest = pstrSource;

   while (*pstrSource != 0)
   {
      if (pstrSource[0] == ',' && pstrSource[1] == ',')
         pstrSource++;

      *pstrDest++ = *pstrSource++;
   }

   refStr.ReleaseBuffer();
   return;
}

to get the job done.

..B ekiM
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
V_BapatCommented:
Try this:

void Replace(CString& SrcStr)
{
   int pos = SrcStr.Find(",,");
   while(pos != -1)
   {
      SrcStr.Delete(pos);
      SrcStr.Find(",,");
   }
}
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

mikeblasCommented:
You'll find V_Bapat's suggestion to be incorrect; it will loop forever because the value of pos is never changed in the body of the loop. Even if you fix it, it will be slower than the example I provided.

..B ekiM
0
chensuCommented:
And CString::Delete may not be there if CString::Replace is not there because they came with the same version of Visual C++ if I remember correctly.
0
knel1234Author Commented:
mikeblas thank you for the assist.  Sorry for the delayed response, I was busy with other tasks.  Your answer did the trick and all is well
knel
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.