避免使用事件或保存到文件的regex撇号

本文关键字:存到文件 regex 撇号 事件 | 更新日期: 2023-09-27 18:18:27

如何避免写入richTextBox保存到文件中的撇号" "answers" ' "

我也试过替换:

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

但是如果文件内容很大,程序会挂起using in events。我也有这个代替删除一次又一次。

因此,最好避免在写入或保存到文件 时写入标记。

好像我做错了:

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

避免使用事件或保存到文件的regex撇号

试试这个:

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