简体   繁体   English

替换文本文档C#中的多个单词

[英]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. 因此,我试图运行一个程序来保存我为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 到目前为止,我可以在添加一组latlong时生成所有文本,但是我似乎无法弄清楚如何在其中添加另一组latlong。

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

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 ii is replaced by the first item in your listBox2 . 我猜问题在于, Replace将替换ii所有出现,因此,如果调试循环,您将看到只有ii第一次被listBox2的第一项listBox2 To solve that,i think you should add the index to ii ,something like this 为了解决这个问题,我认为您应该将索引添加到ii ,就像这样

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 请注意,在第一个循环中,我添加"ii" + i ,在第二个循环中,我替换"ii"+ii

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM