Click here to Skip to main content
15,921,212 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: A question about locking thread Pin
Waldermort4-Sep-06 7:49
Waldermort4-Sep-06 7:49 
QuestionRe: A question about locking thread Pin
Toubou4-Sep-06 18:51
Toubou4-Sep-06 18:51 
AnswerRe: A question about locking thread Pin
Waldermort4-Sep-06 23:35
Waldermort4-Sep-06 23:35 
Questionuser.dmp issue Pin
Rob Caldecott4-Sep-06 6:40
Rob Caldecott4-Sep-06 6:40 
Answerwindbg to the rescue! Pin
Rob Caldecott4-Sep-06 6:58
Rob Caldecott4-Sep-06 6:58 
GeneralRe: windbg to the rescue! Pin
Waldermort4-Sep-06 7:03
Waldermort4-Sep-06 7:03 
Questionreplacing a string with a sub-string Pin
jon-804-Sep-06 6:29
professionaljon-804-Sep-06 6:29 
AnswerRe: replacing a string with a sub-string Pin
Graham Shanks4-Sep-06 12:28
Graham Shanks4-Sep-06 12:28 
The following function removes all occurances of strSearch in sentence:

void StringRemove(std::string& sentence, const char * const strSearch)
{
  size_t len = strlen(strSearch);
  std::string::size_type i = sentence.find(strSearch);
  while(i != std::string::npos)
  {
    sentence.erase(i, len);
    i = sentence.find(strSearch, i);
  }
}


If, as the title of your post suggests you actually want to replace a substring use the following:

void StringReplace(std::string&       sentence, 
                   const char * const strSearch,
                   const char* const  strNew)
{
  size_t len = strlen(strSearch);
  size_t newLen = strlen(strNew);
  std::string::size_type i = sentence.find(strSearch);
  while(i != std::string::npos)
  {
    sentence.replace(i, len, strNew, newLen);
    i = sentence.find(strSearch, i + newLen);
  }
}


To replace all occurances in the vector use:

std::vector<std::string>::iterator i;
for(i = sentences.begin(); i != sentences.end();++i)
  StringReplace(*i, "foo", "bar");


BTW a more succinct version of your count occurances in string function is

unsigned int StringCount(const std::string& sentence, 
                         const char* const  strSearch)
{
  unsigned int count = 0;
  size_t len = strlen(strSearch);
  std::string::size_type i = sentence.find(strSearch);
  while(i != std::string::npos)
  {
    i = sentence.find(strSearch, i + len);
    ++count;
  }
  return count;
}


Graham

My signature is not black, just a very, very dark blue

Questionreduce the size of an array of chars.... Pin
jon-804-Sep-06 6:22
professionaljon-804-Sep-06 6:22 
AnswerRe: reduce the size of an array of chars.... Pin
Waldermort4-Sep-06 6:25
Waldermort4-Sep-06 6:25 
GeneralRe: reduce the size of an array of chars.... Pin
jon-804-Sep-06 6:32
professionaljon-804-Sep-06 6:32 
GeneralRe: reduce the size of an array of chars.... Pin
Waldermort4-Sep-06 6:43
Waldermort4-Sep-06 6:43 
GeneralRe: reduce the size of an array of chars.... Pin
jon-804-Sep-06 8:11
professionaljon-804-Sep-06 8:11 
QuestionControl handles Pin
Oliver1234-Sep-06 6:17
Oliver1234-Sep-06 6:17 
AnswerRe: Control handles Pin
Waldermort4-Sep-06 6:26
Waldermort4-Sep-06 6:26 
AnswerRe: Control handles Pin
gregdolley4-Sep-06 22:21
gregdolley4-Sep-06 22:21 
AnswerRe: Control handles Pin
Hamid_RT11-Sep-06 9:52
Hamid_RT11-Sep-06 9:52 
Questionscanning until the EOF Pin
Desmo164-Sep-06 6:15
Desmo164-Sep-06 6:15 
AnswerRe: scanning until the EOF Pin
Waldermort4-Sep-06 6:30
Waldermort4-Sep-06 6:30 
GeneralRe: scanning until the EOF Pin
Desmo164-Sep-06 6:40
Desmo164-Sep-06 6:40 
GeneralRe: scanning until the EOF Pin
Waldermort4-Sep-06 6:49
Waldermort4-Sep-06 6:49 
Questionneed help Pin
amzeus4-Sep-06 6:04
amzeus4-Sep-06 6:04 
AnswerRe: need help Pin
Cedric Moonen4-Sep-06 6:12
Cedric Moonen4-Sep-06 6:12 
AnswerRe: need help Pin
Hamid_RT4-Sep-06 22:32
Hamid_RT4-Sep-06 22:32 
QuestionSir,How Can Find or See WinMain Function in Simple MFC Application ? Pin
CodeVarma4-Sep-06 5:54
CodeVarma4-Sep-06 5:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.