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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. CString::Replace() problem

CString::Replace() problem

Scheduled Pinned Locked Moved C / C++ / MFC
jsonhelp
12 Posts 4 Posters 1 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.
  • J Jia100

    I am using a code while(csTemp.Replace(":"," <scol> ")).Before this some other locs are also there to replace some other signs,but after some replacement due to this code some string is truncated in the buffer i think...so that rest of the string is not replaced.Pls suggest a solution for this....

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

    Run the replace commands one after another, instead of running them amuck. I hope I understood your query correctly - please explain otherwise.

    It was ever thus, the Neophiles will always rush out and get 'The Latest Thing' at a high price and with all the inherent faults - Dalek Dave.

    J 1 Reply Last reply
    0
    • R Rajesh R Subramanian

      Run the replace commands one after another, instead of running them amuck. I hope I understood your query correctly - please explain otherwise.

      It was ever thus, the Neophiles will always rush out and get 'The Latest Thing' at a high price and with all the inherent faults - Dalek Dave.

      J Offline
      J Offline
      Jia100
      wrote on last edited by
      #3

      Actually these replacement codes are in the same function so they are done back to back...so how can we run them individually?We are taking paragraphs as input & the splitted sentence is actually the strings we are replacing....

      modified on Monday, December 6, 2010 3:06 AM

      R 1 Reply Last reply
      0
      • J Jia100

        Actually these replacement codes are in the same function so they are done back to back...so how can we run them individually?We are taking paragraphs as input & the splitted sentence is actually the strings we are replacing....

        modified on Monday, December 6, 2010 3:06 AM

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

        Without seeing the code, I can hardly tell anything.

        It was ever thus, the Neophiles will always rush out and get 'The Latest Thing' at a high price and with all the inherent faults - Dalek Dave.

        J 1 Reply Last reply
        0
        • R Rajesh R Subramanian

          Without seeing the code, I can hardly tell anything.

          It was ever thus, the Neophiles will always rush out and get 'The Latest Thing' at a high price and with all the inherent faults - Dalek Dave.

          J Offline
          J Offline
          Jia100
          wrote on last edited by
          #5

          This is the function where it is done.... BOOL CProcessData::CleanSentence(CString Sentence, BOOL bPreserveComma) { CString csTemp=Sentence; m_txtCrntStatus->SetWindowText("Cleaning Sentence"); csTemp.Replace("!"," "); csTemp.Replace("<"," "); csTemp.Replace(">"," "); csTemp.Replace("?"," "); csTemp.Replace(";"," "); csTemp.Replace("'"," "); csTemp.Replace("{"," "); csTemp.Replace("}"," "); if(bPreserveComma) { while(csTemp.Replace(","," <comma> ")); while(csTemp.Replace(":"," <scol> ")); } else { while(csTemp.Replace("<comma>","")); } while(csTemp.Replace("\t"," ")); while(csTemp.Replace("\r","")); while(csTemp.Replace("\n","")); while(csTemp.Replace(" "," ")); int nLen =csTemp.GetLength(); if(nLen<2) { while(csTemp.Replace(" ","")); } nLen =csTemp.GetLength(); if(nLen<=0) { return FALSE; } SentenceSplitter(csTemp); return TRUE; }

          R 1 Reply Last reply
          0
          • J Jia100

            This is the function where it is done.... BOOL CProcessData::CleanSentence(CString Sentence, BOOL bPreserveComma) { CString csTemp=Sentence; m_txtCrntStatus->SetWindowText("Cleaning Sentence"); csTemp.Replace("!"," "); csTemp.Replace("<"," "); csTemp.Replace(">"," "); csTemp.Replace("?"," "); csTemp.Replace(";"," "); csTemp.Replace("'"," "); csTemp.Replace("{"," "); csTemp.Replace("}"," "); if(bPreserveComma) { while(csTemp.Replace(","," <comma> ")); while(csTemp.Replace(":"," <scol> ")); } else { while(csTemp.Replace("<comma>","")); } while(csTemp.Replace("\t"," ")); while(csTemp.Replace("\r","")); while(csTemp.Replace("\n","")); while(csTemp.Replace(" "," ")); int nLen =csTemp.GetLength(); if(nLen<2) { while(csTemp.Replace(" ","")); } nLen =csTemp.GetLength(); if(nLen<=0) { return FALSE; } SentenceSplitter(csTemp); return TRUE; }

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

            Nami.v.s wrote:

            while(csTemp.Replace(","," ")); while(csTemp.Replace(":"," ")); } else { while(csTemp.Replace("","")); } while(csTemp.Replace("\t"," ")); while(csTemp.Replace("\r","")); while(csTemp.Replace("\n","")); while(csTemp.Replace(" "," "));

            It makes no sense to run the CString::Replace() within a while. The CString::Replace() call will automatically replace all the occurrences of the given input.

            It was ever thus, the Neophiles will always rush out and get 'The Latest Thing' at a high price and with all the inherent faults - Dalek Dave.

            J L 2 Replies Last reply
            0
            • R Rajesh R Subramanian

              Nami.v.s wrote:

              while(csTemp.Replace(","," ")); while(csTemp.Replace(":"," ")); } else { while(csTemp.Replace("","")); } while(csTemp.Replace("\t"," ")); while(csTemp.Replace("\r","")); while(csTemp.Replace("\n","")); while(csTemp.Replace(" "," "));

              It makes no sense to run the CString::Replace() within a while. The CString::Replace() call will automatically replace all the occurrences of the given input.

              It was ever thus, the Neophiles will always rush out and get 'The Latest Thing' at a high price and with all the inherent faults - Dalek Dave.

              J Offline
              J Offline
              Jia100
              wrote on last edited by
              #7

              k..but then also the problem is not solved...hw can i modify the code to get the whole string replaced properly?

              R D 2 Replies Last reply
              0
              • R Rajesh R Subramanian

                Nami.v.s wrote:

                while(csTemp.Replace(","," ")); while(csTemp.Replace(":"," ")); } else { while(csTemp.Replace("","")); } while(csTemp.Replace("\t"," ")); while(csTemp.Replace("\r","")); while(csTemp.Replace("\n","")); while(csTemp.Replace(" "," "));

                It makes no sense to run the CString::Replace() within a while. The CString::Replace() call will automatically replace all the occurrences of the given input.

                It was ever thus, the Neophiles will always rush out and get 'The Latest Thing' at a high price and with all the inherent faults - Dalek Dave.

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #8

                IMO a looped replace may have a devastating effect, try

                while(csTemp.Replace("a","ab"));

                on a string that holds some 'a'. :)

                Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                1 Reply Last reply
                0
                • J Jia100

                  k..but then also the problem is not solved...hw can i modify the code to get the whole string replaced properly?

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

                  The problem itself is not clear to me. What exactly are you trying to achieve? (provide sample input and desired output). Important: Do not use txtspk here. Please type each word in full.

                  It was ever thus, the Neophiles will always rush out and get 'The Latest Thing' at a high price and with all the inherent faults - Dalek Dave.

                  J 1 Reply Last reply
                  0
                  • R Rajesh R Subramanian

                    The problem itself is not clear to me. What exactly are you trying to achieve? (provide sample input and desired output). Important: Do not use txtspk here. Please type each word in full.

                    It was ever thus, the Neophiles will always rush out and get 'The Latest Thing' at a high price and with all the inherent faults - Dalek Dave.

                    J Offline
                    J Offline
                    Jia100
                    wrote on last edited by
                    #10

                    This is the part of a parser...so first cleaning the sentence.The input will be .rtf documents.For example if there is a sentence like Name:XXXX,HomeLoc:WF etc. it will be the text that will be got from the document.So these symbols like ':' are replaced by like that i had mentioned before.The desired output should be like...Name<scol>XXXX <comma>HomeLoc<scol>WF etc.Hope now the question is clear.

                    R 1 Reply Last reply
                    0
                    • J Jia100

                      k..but then also the problem is not solved...hw can i modify the code to get the whole string replaced properly?

                      D Offline
                      D Offline
                      David Crow
                      wrote on last edited by
                      #11

                      Either you are not understanding what you are asking, or you are not understanding what you've been told. Using the Replace() method within a while() loop is not going to work in your situation. Other than that, if it's still not working, you need to show the input string, the desired output string, and the output string that your code produces.

                      "One man's wage rise is another man's price increase." - Harold Wilson

                      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                      "Man who follows car will be exhausted." - Confucius

                      1 Reply Last reply
                      0
                      • J Jia100

                        This is the part of a parser...so first cleaning the sentence.The input will be .rtf documents.For example if there is a sentence like Name:XXXX,HomeLoc:WF etc. it will be the text that will be got from the document.So these symbols like ':' are replaced by like that i had mentioned before.The desired output should be like...Name<scol>XXXX <comma>HomeLoc<scol>WF etc.Hope now the question is clear.

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

                        Nami.v.s wrote:

                        Name:XXXX,HomeLoc:WF

                        Nami.v.s wrote:

                        Name<scol>XXXX <comma>HomeLoc<scol>WF

                        First replace all the commas with a single call to CString::Replace(). Next, replace all the colons with another single call to CString::Replace() If there are more such characters, repeat. You should never need a while here.

                        It was ever thus, the Neophiles will always rush out and get 'The Latest Thing' at a high price and with all the inherent faults - Dalek Dave.

                        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