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. std:string

std:string

Scheduled Pinned Locked Moved C / C++ / MFC
questiontutorial
7 Posts 4 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
    Shamoon
    wrote on last edited by
    #1

    I want to replace the last character of a string represented by std::string with comma. How can i do so ?? Example: std::string mystring("Hellow World"); I want to replace last character of "Hellow World" i.e 'd' by comma "'"

    R M 2 Replies Last reply
    0
    • S Shamoon

      I want to replace the last character of a string represented by std::string with comma. How can i do so ?? Example: std::string mystring("Hellow World"); I want to replace last character of "Hellow World" i.e 'd' by comma "'"

      R Offline
      R Offline
      ReX
      wrote on last edited by
      #2

      I wrote this function for this purpose. I hope this helps. inline void Replace(std::string* pstr, const char old_char, char* pnew_char) { if(!pstr->empty()) { int n = 0; while((n = pstr->find(old_char)) != -1) { *pstr = pstr->erase(n, 1); *pstr = pstr->insert(n, pnew_char); } } } Usage: Replace(&mystring, 'd', ','); For the last character only you can take: mystring.erase(mystring.length(), 1); mystring.append(','); Greetings ReX

      M 1 Reply Last reply
      0
      • S Shamoon

        I want to replace the last character of a string represented by std::string with comma. How can i do so ?? Example: std::string mystring("Hellow World"); I want to replace last character of "Hellow World" i.e 'd' by comma "'"

        M Offline
        M Offline
        mishgun
        wrote on last edited by
        #3

        *mystring.rbegin() = '.'; it works :) nobody is perfect

        M 1 Reply Last reply
        0
        • M mishgun

          *mystring.rbegin() = '.'; it works :) nobody is perfect

          M Offline
          M Offline
          Mike Nordell
          wrote on last edited by
          #4

          Isn't that to be *(++mystring.rbegin()) = '.'; ?

          M 1 Reply Last reply
          0
          • R ReX

            I wrote this function for this purpose. I hope this helps. inline void Replace(std::string* pstr, const char old_char, char* pnew_char) { if(!pstr->empty()) { int n = 0; while((n = pstr->find(old_char)) != -1) { *pstr = pstr->erase(n, 1); *pstr = pstr->insert(n, pnew_char); } } } Usage: Replace(&mystring, 'd', ','); For the last character only you can take: mystring.erase(mystring.length(), 1); mystring.append(','); Greetings ReX

            M Offline
            M Offline
            Mike Nordell
            wrote on last edited by
            #5

            That's also a way to burn CPU cycles. Another (possibly better) way to do it could be std::replace(string.begin(), string.end(), 'a', 'b'); to replace all instances of 'a' with 'b'.

            1 Reply Last reply
            0
            • M Mike Nordell

              Isn't that to be *(++mystring.rbegin()) = '.'; ?

              M Offline
              M Offline
              mishgun
              wrote on last edited by
              #6

              nope :) rbegin() returns last member of container, as i understand it :))) nobody is perfect

              M 1 Reply Last reply
              0
              • M mishgun

                nope :) rbegin() returns last member of container, as i understand it :))) nobody is perfect

                M Offline
                M Offline
                Mike Nordell
                wrote on last edited by
                #7

                Then, AFAIK, your understanding is wrong. The doc's I've seen says "The rbegin member function returns a reverse iterator that points just beyond the end of the controlled sequence" and every experience I have with rbegin() says the same.

                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