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!

Since replaceAll() returns void and doesn't change any global variables or state. What does this function accomplish?

Edited 2 Years Ago by gerard4143

It doesn't accomplish anything, I know it should be of type String, and that it should modify the string, but beyond that, I am not sure what the modified string that I am returning is

Well the question I would ask is - Do you want to modify a variable past to this function or do you want to return a variable created by this function?

Then pass it as a reference.

Something like this

#include <iostream>
#include <string>

void replaceAll(const std::string & oldString, const std::string & newString, std::string & original)
{
    size_t n = 0;
    while ((n = original.find(oldString, n)) != std::string::npos)
    {
        original.replace(n, oldString.size(), newString);
        n += newString.size();
    }
}
int main(int argc, char** argv)
{
    std::string foo("T44 T44 T44 bar");

    replaceAll("T44", "4/4", foo);

    std::cout << foo << std::endl;

    return 0;
}

Edited 2 Years Ago by gerard4143

Sorry my brain is mush from staring at code for too long.

Here is my solution:

    string replaceAll(string const & oldString, string const & newString, string & const original){
        size_t n = 0;
        string replaced;

        while ((n = original.find(oldString, n)) != std::string::npos){
            replaced = original.replace(n, oldString.size(), newString);
            replaced;
            n += newString.size();
        }
        return replaced;
    }
This article has been dead for over six months. Start a new discussion instead.