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. C / C++ / MFC
  4. how to change one BYTE in CString

how to change one BYTE in CString

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
11 Posts 6 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.
  • H Offline
    H Offline
    hanlei0000000009
    wrote on last edited by
    #1

    Hi, all CString cstr = "abcd"; LPTSTR p = cstr.GetBuffer(); p[2] = 'r'; // cstr is abcd, but not is abrd I want to change 'c' to 'r' in cstr. How to do?

    N M 2 Replies Last reply
    0
    • H hanlei0000000009

      Hi, all CString cstr = "abcd"; LPTSTR p = cstr.GetBuffer(); p[2] = 'r'; // cstr is abcd, but not is abrd I want to change 'c' to 'r' in cstr. How to do?

      N Offline
      N Offline
      Naveen
      wrote on last edited by
      #2

      hanlei0000000009 wrote:

      // cstr is abcd, but not is abrd

      CString cstr = "abcd"; cstr.SetAt( 2,'r');

      hanlei0000000009 wrote:

      I want to change 'c' to 'r' in cstr.

      But your purpose is to replace the c with r, you can use the Replace function.. CString cstr = "abcd"; cstr.Replace( 'c','r');

      nave [OpenedFileFinder]

      modified on Thursday, March 27, 2008 11:37 PM

      1 Reply Last reply
      0
      • H hanlei0000000009

        Hi, all CString cstr = "abcd"; LPTSTR p = cstr.GetBuffer(); p[2] = 'r'; // cstr is abcd, but not is abrd I want to change 'c' to 'r' in cstr. How to do?

        M Offline
        M Offline
        Mark Salsbery
        wrote on last edited by
        #3

        You rarely need to use GetBuffer().  The CString class has most functions you need, including SetAt()...

        CString cstr = _T("abcd");
            cstr.SetAt(2, _T('r'));

        If you insist on using GetBuffer(), remember: "If you use the pointer returned by GetBuffer to change the string contents, you must call ReleaseBuffer ..."

        CString cstr = _T("abcd");
            LPTSTR p = cstr.GetBuffer();
            p[2] = _T('r');
            cstr.ReleaseBuffer();

        Mark

        Mark Salsbery Microsoft MVP - Visual C++ :java:

        CPalliniC 1 Reply Last reply
        0
        • M Mark Salsbery

          You rarely need to use GetBuffer().  The CString class has most functions you need, including SetAt()...

          CString cstr = _T("abcd");
              cstr.SetAt(2, _T('r'));

          If you insist on using GetBuffer(), remember: "If you use the pointer returned by GetBuffer to change the string contents, you must call ReleaseBuffer ..."

          CString cstr = _T("abcd");
              LPTSTR p = cstr.GetBuffer();
              p[2] = _T('r');
              cstr.ReleaseBuffer();

          Mark

          Mark Salsbery Microsoft MVP - Visual C++ :java:

          CPalliniC Online
          CPalliniC Online
          CPallini
          wrote on last edited by
          #4

          :laugh:

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

          In testa che avete, signor di Ceprano?

          R T 2 Replies Last reply
          0
          • CPalliniC CPallini

            :laugh:

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

            R Offline
            R Offline
            Rajesh R Subramanian
            wrote on last edited by
            #5

            Someone said to me that in the next version of MFC, this function would be renamed to GetBufferIReallyKnowWhatImDoing() :laugh:

            Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP

            CPalliniC 1 Reply Last reply
            0
            • CPalliniC CPallini

              :laugh:

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

              T Offline
              T Offline
              ThatsAlok
              wrote on last edited by
              #6

              i will take ReleaseBuffer revenge!

              "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
              Never mind - my own stupidity is the source of every "problem" - Mixture

              cheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You/codeProject$$>

              1 Reply Last reply
              0
              • R Rajesh R Subramanian

                Someone said to me that in the next version of MFC, this function would be renamed to GetBufferIReallyKnowWhatImDoing() :laugh:

                Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP

                CPalliniC Online
                CPalliniC Online
                CPallini
                wrote on last edited by
                #7

                ..and they will also add the method PlzGetBuf, always throwing by design. :)

                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

                In testa che avete, signor di Ceprano?

                T R 2 Replies Last reply
                0
                • CPalliniC CPallini

                  ..and they will also add the method PlzGetBuf, always throwing by design. :)

                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                  This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

                  T Offline
                  T Offline
                  ThatsAlok
                  wrote on last edited by
                  #8

                  CPallini wrote:

                  ..and they will also add the method PlzGetBuf, always throwing by design.

                  One more GiveMeBufOtherwiseOsama

                  "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
                  Never mind - my own stupidity is the source of every "problem" - Mixture

                  cheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You/codeProject$$>

                  1 Reply Last reply
                  0
                  • CPalliniC CPallini

                    ..and they will also add the method PlzGetBuf, always throwing by design. :)

                    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                    This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

                    R Offline
                    R Offline
                    Rajesh R Subramanian
                    wrote on last edited by
                    #9

                    CPallini wrote:

                    PlzGetBuf

                    Does it take a boolean parameter too? PlzGetBuf(BOOL bUrgent) :rolleyes:

                    Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP

                    CPalliniC 1 Reply Last reply
                    0
                    • R Rajesh R Subramanian

                      CPallini wrote:

                      PlzGetBuf

                      Does it take a boolean parameter too? PlzGetBuf(BOOL bUrgent) :rolleyes:

                      Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP

                      CPalliniC Online
                      CPalliniC Online
                      CPallini
                      wrote on last edited by
                      #10

                      Rajesh R Subramanian wrote:

                      Does it take a boolean parameter too? PlzGetBuf(BOOL bUrgent)

                      Actually it does, however it is optional:

                      PXSTR PlzGetBuf(BOOL bUrgent = TRUE);

                      :-D

                      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

                      In testa che avete, signor di Ceprano?

                      R 1 Reply Last reply
                      0
                      • CPalliniC CPallini

                        Rajesh R Subramanian wrote:

                        Does it take a boolean parameter too? PlzGetBuf(BOOL bUrgent)

                        Actually it does, however it is optional:

                        PXSTR PlzGetBuf(BOOL bUrgent = TRUE);

                        :-D

                        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

                        R Offline
                        R Offline
                        Rajesh R Subramanian
                        wrote on last edited by
                        #11

                        :laugh:

                        Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP

                        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