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. replaceing characters in a string

replaceing characters in a string

Scheduled Pinned Locked Moved C / C++ / MFC
help
7 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.
  • S Offline
    S Offline
    si_69
    wrote on last edited by
    #1

    Hi, can ne1 help if i had a string, mystring = "0933221212" and i wanted to replace the first two characters "09" with say "012" so i was left with "01233221212" whats the best way of doing this thanks loads si

    T R 2 Replies Last reply
    0
    • S si_69

      Hi, can ne1 help if i had a string, mystring = "0933221212" and i wanted to replace the first two characters "09" with say "012" so i was left with "01233221212" whats the best way of doing this thanks loads si

      T Offline
      T Offline
      Toni78
      wrote on last edited by
      #2

      CString mystring = "0933221212"; mystring.Replace( "09", "012" ); // Afterall, I realized that even my comment lines have bugs When one cannot invent, one must at least improve (in bed).-My latest fortune cookie

      S 1 Reply Last reply
      0
      • T Toni78

        CString mystring = "0933221212"; mystring.Replace( "09", "012" ); // Afterall, I realized that even my comment lines have bugs When one cannot invent, one must at least improve (in bed).-My latest fortune cookie

        S Offline
        S Offline
        si_69
        wrote on last edited by
        #3

        thanks for the quick reply the problem with what you sent me, is that it will replace all occurrances of 09 with 012, i need it to replace only one occurance of 09 which will always be the first two characters thanks si

        J M 2 Replies Last reply
        0
        • S si_69

          thanks for the quick reply the problem with what you sent me, is that it will replace all occurrances of 09 with 012, i need it to replace only one occurance of 09 which will always be the first two characters thanks si

          J Offline
          J Offline
          JensB
          wrote on last edited by
          #4

          CString mystring = "0933221212"; mystring.Replace( "09", "012" ); CString strRightPart = "012" + (mystring.Right(mystring.GetLenght()-2)); ::AfxMessageBox(strRightPart); I think it's something like this. Best regards Jens

          1 Reply Last reply
          0
          • S si_69

            thanks for the quick reply the problem with what you sent me, is that it will replace all occurrances of 09 with 012, i need it to replace only one occurance of 09 which will always be the first two characters thanks si

            M Offline
            M Offline
            Manfred Staiger
            wrote on last edited by
            #5

            if myString.Right(2)=="09" { myString.Delete(0, 2); myString.Insert(0, "123"); } MS

            S 1 Reply Last reply
            0
            • M Manfred Staiger

              if myString.Right(2)=="09" { myString.Delete(0, 2); myString.Insert(0, "123"); } MS

              S Offline
              S Offline
              Simon W 0
              wrote on last edited by
              #6

              myString.TrimLeft(_T("09")); myString.Insert(0, _T("123")); I am seeking... For what? Why did you ask me for what? I don't know!

              1 Reply Last reply
              0
              • S si_69

                Hi, can ne1 help if i had a string, mystring = "0933221212" and i wanted to replace the first two characters "09" with say "012" so i was left with "01233221212" whats the best way of doing this thanks loads si

                R Offline
                R Offline
                Ryan Binns
                wrote on last edited by
                #7

                If you want to replace the first two characters, then do this:

                mystring = _T("012") + mystring.Mid(2);

                Hope this helps,

                Ryan

                "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

                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