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. changing a part of a string

changing a part of a string

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
11 Posts 5 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.
  • G Offline
    G Offline
    Green Fuze
    wrote on last edited by
    #1

    hello everybody! I'm having trouble in changing a certain string. if I have have that string: strNum = 1234&567; the thing is that I want to change the "&" sign to the "and" so my string will be 1234and567. can anyone help me with that somehow??? :confused: Thanks!

    C N N T 4 Replies Last reply
    0
    • G Green Fuze

      hello everybody! I'm having trouble in changing a certain string. if I have have that string: strNum = 1234&567; the thing is that I want to change the "&" sign to the "and" so my string will be 1234and567. can anyone help me with that somehow??? :confused: Thanks!

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      Does CString have a Replace function ? I don't think that std::string does. Either way, either you or a wrapper needs to allocate new memory and construct a new string ( char * ), because you want to replace one character with 3. Christian I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer

      1 Reply Last reply
      0
      • G Green Fuze

        hello everybody! I'm having trouble in changing a certain string. if I have have that string: strNum = 1234&567; the thing is that I want to change the "&" sign to the "and" so my string will be 1234and567. can anyone help me with that somehow??? :confused: Thanks!

        N Offline
        N Offline
        ng kok chuan
        wrote on last edited by
        #3

        you should tokenize the string with a delimiter of your choice, then append the tokens back into your string. // code for tokenizing, taken from http://oopweb.com/CPP/Documents/CPPHOWTO/Volume/C++Programming-HOWTO-7.html[^] void Tokenize(const string& str, vector& tokens, const string& delimiters = " ") { // Skip delimiters at beginning. string::size_type lastPos = str.find_first_not_of(delimiters, 0); // Find first "non-delimiter". string::size_type pos = str.find_first_of(delimiters, lastPos); while (string::npos != pos || string::npos != lastPos) { // Found a token, add it to the vector. tokens.push_back(str.substr(lastPos, pos - lastPos)); // Skip delimiters. Note the "not_of" lastPos = str.find_first_not_of(delimiters, pos); // Find next "non-delimiter" pos = str.find_first_of(delimiters, lastPos); } }

        G 1 Reply Last reply
        0
        • N ng kok chuan

          you should tokenize the string with a delimiter of your choice, then append the tokens back into your string. // code for tokenizing, taken from http://oopweb.com/CPP/Documents/CPPHOWTO/Volume/C++Programming-HOWTO-7.html[^] void Tokenize(const string& str, vector& tokens, const string& delimiters = " ") { // Skip delimiters at beginning. string::size_type lastPos = str.find_first_not_of(delimiters, 0); // Find first "non-delimiter". string::size_type pos = str.find_first_of(delimiters, lastPos); while (string::npos != pos || string::npos != lastPos) { // Found a token, add it to the vector. tokens.push_back(str.substr(lastPos, pos - lastPos)); // Skip delimiters. Note the "not_of" lastPos = str.find_first_not_of(delimiters, pos); // Find next "non-delimiter" pos = str.find_first_of(delimiters, lastPos); } }

          G Offline
          G Offline
          Green Fuze
          wrote on last edited by
          #4

          Thanks! :-) understood!

          1 Reply Last reply
          0
          • G Green Fuze

            hello everybody! I'm having trouble in changing a certain string. if I have have that string: strNum = 1234&567; the thing is that I want to change the "&" sign to the "and" so my string will be 1234and567. can anyone help me with that somehow??? :confused: Thanks!

            N Offline
            N Offline
            namaskaaram
            wrote on last edited by
            #5

            have u tried strstr() of the string.h ?....itz used to find a string inside a srring!!!!! cheerz.....:-D "faith, hope, love remain, these three.....; but the greatest of these is love" -1 Corinthians 13:13

            G 1 Reply Last reply
            0
            • G Green Fuze

              hello everybody! I'm having trouble in changing a certain string. if I have have that string: strNum = 1234&567; the thing is that I want to change the "&" sign to the "and" so my string will be 1234and567. can anyone help me with that somehow??? :confused: Thanks!

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

              If this String is Object of CString then you simply use this Function CString strNum = "1234&567"; strNum.Replace("&","and");


              [Vote One Here, Complete my Survey....] Alok Gupta
              visit me at http://www.thisisalok.tk          "I Think Believe this Will Help"

              G N 2 Replies Last reply
              0
              • T ThatsAlok

                If this String is Object of CString then you simply use this Function CString strNum = "1234&567"; strNum.Replace("&","and");


                [Vote One Here, Complete my Survey....] Alok Gupta
                visit me at http://www.thisisalok.tk          "I Think Believe this Will Help"

                G Offline
                G Offline
                Green Fuze
                wrote on last edited by
                #7

                yeah I know, but I was looking for string, not CString. but, I don't quite understand the difference between the two. I know that CString is microsoft's string class, and it has more functionality, but what are the disadvantages of CString comparing to string?

                T 1 Reply Last reply
                0
                • N namaskaaram

                  have u tried strstr() of the string.h ?....itz used to find a string inside a srring!!!!! cheerz.....:-D "faith, hope, love remain, these three.....; but the greatest of these is love" -1 Corinthians 13:13

                  G Offline
                  G Offline
                  Green Fuze
                  wrote on last edited by
                  #8

                  now thats a useful info! :-)

                  1 Reply Last reply
                  0
                  • G Green Fuze

                    yeah I know, but I was looking for string, not CString. but, I don't quite understand the difference between the two. I know that CString is microsoft's string class, and it has more functionality, but what are the disadvantages of CString comparing to string?

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

                    Cstring is MFC dependent class whereas STL string as no such dependency!


                    [Vote One Here, Complete my Survey....] Alok Gupta
                    visit me at http://www.thisisalok.tk          "I Think Believe this Will Help"

                    1 Reply Last reply
                    0
                    • T ThatsAlok

                      If this String is Object of CString then you simply use this Function CString strNum = "1234&567"; strNum.Replace("&","and");


                      [Vote One Here, Complete my Survey....] Alok Gupta
                      visit me at http://www.thisisalok.tk          "I Think Believe this Will Help"

                      N Offline
                      N Offline
                      ng kok chuan
                      wrote on last edited by
                      #10

                      i've checked the string class, and it also contains a replace member function, so essentially you can do the same thing that alok said for the CString class. anyway here's more definitions for the string class: http://www.cppreference.com/cppstring/[^]

                      T 1 Reply Last reply
                      0
                      • N ng kok chuan

                        i've checked the string class, and it also contains a replace member function, so essentially you can do the same thing that alok said for the CString class. anyway here's more definitions for the string class: http://www.cppreference.com/cppstring/[^]

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

                        ng kok chuan wrote: _anyway here's more definitions for the string class: http://www.cppreference.com/cppstring/\[^\]_ Looks Good Thanks

                        "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

                        cheers, Alok Gupta

                        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