-1

嗨,我正在尝试查找和替换字符串。问题是String.Replace函数需要两个参数来表示旧值和新值。我需要类似的东西

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



            }

        }
4

1 回答 1

3

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

content = content.Replace(the-value-you-want-to-replace, the-value-you-want-to-replace-it-with);
于 2013-08-14T11:25:10.037 回答