CString::Replace() problem
-
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....
-
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....
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.
-
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.
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
-
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
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.
-
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.
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; }
-
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; }
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 awhile
. TheCString::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.
-
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 awhile
. TheCString::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.
-
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 awhile
. TheCString::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.
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.
-
k..but then also the problem is not solved...hw can i modify the code to get the whole string replaced properly?
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.
-
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.
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.
-
k..but then also the problem is not solved...hw can i modify the code to get the whole string replaced properly?
Either you are not understanding what you are asking, or you are not understanding what you've been told. Using the
Replace()
method within awhile()
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
-
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.
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 toCString::Replace()
If there are more such characters, repeat. You should never need awhile
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.