I need to find a string and edit its formatting.
So far I can replace the first occurrence of a line, but I cannot do this with the next occurrences of this line.
This is what I have, like:
if(chartDataString.find("*A") == string::npos){ return;} else{chartDataString.replace(chartDataString.find("*A"), 3,"[A]\n");}
If it does not find the string, it does not print anything at all, so this is not good.
I know I need to skip the entire chartDataString line and replace all occurrences. I know that there are many similar messages, but I do not understand (for example, Replace a substring with another C ++ substring )
I also tried to do something like this to iterate over a string:
string toSearch = chartDataString; string toFind = "*A:"; for (int i = 0; i<toSearch.length() - toFind.length(); i++){ if(toSearch.substr(i, toFind.length()) == toFind){ chartDataString.replace(chartDataString.find(toFind), 3, "[A]\n"); } }
EDIT given the suggestions, this should theoretically work, but I don't know why this is not
size_t startPos=0; string myString = "*A"; while(string::npos != (startPos = chartDataString.find(myString, startPos))){ chartDataString.replace(chartDataString.find(myString, startPos), 3, "*A\n"); startPos = startPos + myString.length(); }