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?

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?

I want to modify a variable passed to this variable. That variable is chartDataString

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;
}

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;
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.