Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. ATL / WTL / STL
  4. How to replace a '%' character in CComBSTR with "%%"

How to replace a '%' character in CComBSTR with "%%"

Scheduled Pinned Locked Moved ATL / WTL / STL
tutorialquestion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    narayanagvs
    wrote on last edited by
    #1

    Hi I just wanted to replace a '%' character in CComBSTR with "%%". But the code does not support CString. The solution should be UNICODE support. Any methods to perform this operation ? Thanks In Advance. Babu

    Today is a gift, that's why it is called the present.

    C 1 Reply Last reply
    0
    • N narayanagvs

      Hi I just wanted to replace a '%' character in CComBSTR with "%%". But the code does not support CString. The solution should be UNICODE support. Any methods to perform this operation ? Thanks In Advance. Babu

      Today is a gift, that's why it is called the present.

      C Offline
      C Offline
      Cool_Dev
      wrote on last edited by
      #2

      CComBstr class doesn't have a method to replace a sub-string with another or to insert another string in specified position. All you can do is, get BSTR which is wrapped in CComBSTR class ( either by m_str member or by operator BSTR). You need to copy this to another CComBSTR. You may use its Append() method or overloaded += operators for ease.

      CComBSTR str("here%are%percentages%");
      CComBSTR newstr;

      WCHAR split[] = L"%";
      WCHAR* pos = wcstok(str.m_str, split);
      while(0 != pos) {
      newstr.Append(pos);
      newstr.Append(L"%%");

      pos = wcstok(NULL, split);
      }

      NOTE: I would like to remind that BSTR data type has preceding header components, and the final string is terminated with double null characters. This is how system allocates memory for BSTR. There can be embedded single null characters in BSTR string data. In that case, wcslen() and wcstok() functions won't work as expected. Use CComBSTR::Length() or SysStringLen() to get length of string. You may need to examine each character in string till you find two continuous '\0' characters. During this, whenever you find a '%' add an additional '%' more tho the new string. :|
      Unicode support is completely ensured. :)

      modified on Thursday, March 24, 2011 9:09 AM

      B 1 Reply Last reply
      0
      • C Cool_Dev

        CComBstr class doesn't have a method to replace a sub-string with another or to insert another string in specified position. All you can do is, get BSTR which is wrapped in CComBSTR class ( either by m_str member or by operator BSTR). You need to copy this to another CComBSTR. You may use its Append() method or overloaded += operators for ease.

        CComBSTR str("here%are%percentages%");
        CComBSTR newstr;

        WCHAR split[] = L"%";
        WCHAR* pos = wcstok(str.m_str, split);
        while(0 != pos) {
        newstr.Append(pos);
        newstr.Append(L"%%");

        pos = wcstok(NULL, split);
        }

        NOTE: I would like to remind that BSTR data type has preceding header components, and the final string is terminated with double null characters. This is how system allocates memory for BSTR. There can be embedded single null characters in BSTR string data. In that case, wcslen() and wcstok() functions won't work as expected. Use CComBSTR::Length() or SysStringLen() to get length of string. You may need to examine each character in string till you find two continuous '\0' characters. During this, whenever you find a '%' add an additional '%' more tho the new string. :|
        Unicode support is completely ensured. :)

        modified on Thursday, March 24, 2011 9:09 AM

        B Offline
        B Offline
        barneyman
        wrote on last edited by
        #3

        or ...

        CComBSTR str("here%are%percentages%");
        CString workingString(str);
        workingString.Replace(_T("%"),_T("%%"));
        str=workingString;

        C 1 Reply Last reply
        0
        • B barneyman

          or ...

          CComBSTR str("here%are%percentages%");
          CString workingString(str);
          workingString.Replace(_T("%"),_T("%%"));
          str=workingString;

          C Offline
          C Offline
          Cool_Dev
          wrote on last edited by
          #4

          Its ok. But I guess you didn't notice "But the code does not support CString" in the actual question :)

          B 1 Reply Last reply
          0
          • C Cool_Dev

            Its ok. But I guess you didn't notice "But the code does not support CString" in the actual question :)

            B Offline
            B Offline
            barneyman
            wrote on last edited by
            #5

            sorry - rookie error - I'll pay more attention you nailed it then :)

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups