I am trying to replace a bunch of substrings with another bunch of substrings. I've got that working great, but now my code is a mess. This is what I have :
void parseChartData(string chartDataString){
if(!chartDataString.empty()){
chartData.clear();
//replaceAll("T44" , "4/4", chartDataString);
string s = "*A";
string t = "[A]\n";
string::size_type n = 0;
while ( ( n = chartDataString.find( s, n ) ) != std::string::npos ){
chartDataString.replace( n, s.size(), t );
n += t.size();
}
s = "*B";
t = "[B]\n";
n = 0;
while ( ( n = chartDataString.find( s, n ) ) != std::string::npos ){
chartDataString.replace( n, s.size(), t );
n += t.size();
}
etc
chartData.append(chartDataString);
}
I would like to clean up the code and put it all into a function and then call that function within this function. This is what I have tried to do:
void replaceAll(string oldString, string newString, string original){
size_t n = 0;
while ((n = original.find(oldString, n)) != std::string::npos){
original.replace(n, oldString.size(), newString);
n += newString.size();
}
}
and then call replaceAll("*A", "[A]\n", chartDataString);
This unfortunately doesn't work, and I am not sure how to fix it... any ideas?
Thanks in advance!