关于查找和替换的困惑

时间:2013-08-14 11:02:44

标签: c# replace

您好我正在尝试查找和替换字符串。问题是String.Replace函数需要两个参数来表示oldvalue和new value。我需要类似的东西

 (content, textBox1.Text, textBox1.Text)

我尝试使用Regex,但它没有用

   private void button1_Click(object sender, EventArgs e)
        {

        openFileDialog1.Filter = "All Files|*.*|Images Files(*.jpeg)|*.jpeg";
        //openFileDialog1.Multiselect = true;

        DialogResult dr = openFileDialog1.ShowDialog();
        if (dr == DialogResult.OK)
        {


                StreamReader sr = new StreamReader(openFileDialog1.FileName, Encoding.GetEncoding("Windows-1254"));
                string content = sr.ReadToEnd();

                sr.Close();

                content = Regex.Replace(content, textBox1.Text, textBox1.Text);

                StreamWriter sw = new StreamWriter(content);
                sw.Write(content);
                sw.Close();



            }

        }

1 个答案:

答案 0 :(得分:3)

你想用什么取代? string.Replace工作正常,但是你用自己替换textBox1.Text,这显然会导致相同的字符串。你需要这样的东西:

content = content.Replace(the-value-you-want-to-replace, the-value-you-want-to-replace-it-with);