Christopher Harvey
Christopher Harvey

Reputation: 15

Replace multiples words in text document C#

So I'm trying to get a program running to save me copying and pasting loads of text for android studio. For this I have created listboxes with all the different bits of information required, added button click event to create a document, and another button click event to add the text into the document. So far I'm able to generate all the text when adding one set of latlongs, but I can't seem to work out how to add another set latlongs in.. For example

I need:

googleMap.addMarker(new MarkerOptions().position(new LatLng(-17.79940000000, 31.01680000000)).title(bbb));

googleMap.addMarker(new MarkerOptions().position(new LatLng(-17.80150000000, 
31.03650000000)).title(ccc));

But all's I am getting is:

googleMap.addMarker(new MarkerOptions().position(new LatLng(-17.79940000000, 31.01680000000)).title(bbb));

googleMap.addMarker(new MarkerOptions().position(new LatLng(-17.80150000000, 31.01680000000)).title(bbb));

The Longitude value is not changing? I'm hoping all of this makes sense?

string path = Environment.CurrentDirectory + "/" + "latlong.txt";

    private void button1_Click(object sender, EventArgs e)
    {
        if (!File.Exists(path))
        {
            File.CreateText(path);
            MessageBox.Show("File has been created.");
        }
    }

    private void button2_Click(object sender, EventArgs e)
    {
        using (StreamWriter stwr = new StreamWriter(path))
        {
            for (int i = 0; i < listBox1.Items.Count; i++)
            {
                stwr.WriteLine("googleMap.addMarker(new MarkerOptions().position(new LatLng(" + listBox1.Items[i] + ", " + "ii" + ")).title(" + "bbb" + "));");
            }
            stwr.Close();
            string text = File.ReadAllText("latlong.txt");
            for (int ii = 0; ii < listBox2.Items.Count; ii++)
            {

                text = text.Replace("ii", Convert.ToString(listBox2.Items[ii]));
            }
            File.WriteAllText("latlong.txt", text);
        }
    }

Upvotes: 0

Views: 67

Answers (1)

Pikoh
Pikoh

Reputation: 7713

I guess the problem is that Replace is replacing all occurences of ii, so if you debug your loop you'll see that only the first time iiis replaced by the first item in your listBox2. To solve that,i think you should add the index to ii,something like this

private void button2_Click(object sender, EventArgs e)
{
    using (StreamWriter stwr = new StreamWriter(path))
    {
        for (int i = 0; i < listBox1.Items.Count; i++)
        {
            stwr.WriteLine("googleMap.addMarker(new MarkerOptions().position(new LatLng(" + listBox1.Items[i] + ", " + "ii" + i + ")).title(" + "bbb" + "));");
        }
        stwr.Close();
        string text = File.ReadAllText("latlong.txt");
        for (int ii = 0; ii < listBox2.Items.Count; ii++)
        {

            text = text.Replace("ii"+ii, Convert.ToString(listBox2.Items[ii]));
        }
        File.WriteAllText("latlong.txt", text);
    }
}

Notice that in the first loop i'm adding "ii" + i and in the second i'm replacing "ii"+ii

Upvotes: 2

Related Questions