避免使用带有事件的正则表达式或保存到文件中的撇号

Avoid apostrophe with regex with event or saving to file

如何避免为撇号“”和“'”写入保存到文件的richTextBox

我也试过替换:

string text = File.ReadAllText(path); 
text = text.Replace("’", "").Replace("'", "");
File.WriteAllText(path, text.ToLower());

但是如果文件内容很大,程序会在事件中使用时挂起。我也有这个 而不是一次又一次地删除。

所以最好避免在写入或保存到文件时写入标记

好像我做错了:

string toFile = String.Join(" ", richTextBox1.Lines); 
var pat1 = @"\s?(’|')\s?"; 
var out1 = Regex.Replace(toFile, pat1, "");
File.WriteAllText(path, out1.ToLower()); 

所以如果粘贴文本并将整个文本放在一个字符串中,这样我就会丢失行。

但是想要得到这个结果,如果插入是:

Could’ve
Couldn’t
Didn’t
Doesn’t

我想像这样将它写入文件:

couldve
couldnt
didnt
doesnt

试试这个:

System.Windows.Forms.OpenFileDialog oFile = new System.Windows.Forms.OpenFileDialog();

oFile.InitialDirectory = "c:\" ;
oFile.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" ;
oFile.FilterIndex = 2 ;
oFile.RestoreDirectory = true ;

if(oFile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
    string file = oFile.Filename;
    string tmp = file + ".tmp";

    if (System.IO.File.Exists(tmp)) 
        System.IO.File.Delete(tmp);

    using(System.IO.StreamReader sr = new System.IO.StreamReader(file)) 
    using(System.IO.StreamWriter sw = new System.IO.StreamWriter(tmp, false, Encoding.ASCII ))
    {
        string line = null;
        while((line = sr.ReadLine()) != null)
            sw.WriteLine(line.Replace("’", "").Replace("'", ""));
    } 

    System.IO.File.Delete(file);
    System.IO.File.Move(tmp,  file);
}