[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 656
  • Last Modified:

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
0
knel1234
Asked:
knel1234
1 Solution
 
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
 
V_BapatCommented:
Try this:

void Replace(CString& SrcStr)
{
   int pos = SrcStr.Find(",,");
   while(pos != -1)
   {
      SrcStr.Delete(pos);
      SrcStr.Find(",,");
   }
}
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now