2012-04-18 61 views
0

目前,我正在構建一個帶有額外選項的議程。用 r n替換文本文件中的文本

用於測試目的我存儲在一個簡單的.txt文件 數據(之後,它會連接到一個虛擬助理的議程。)

要更改或從本txt文件我已經刪除文本一個問題。 儘管需要替換的部分內容和搜索字符串完全相同,但並不代替內容中的文本。

代碼:

更改方法

public override void Change(List<object> oldData, List<object> newData) 
    { 
     int index = -1; 
     for (int i = 0; i < agenda.Count; i++) 
     { 
      if(agenda[i].GetType() == "Task") 
      { 
       Task t = (Task)agenda[i]; 

       if(t.remarks == oldData[0].ToString() && t.datetime == (DateTime)oldData[1] && t.reminders == oldData[2]) 
       { 
        index = i; 
        break; 
       } 
      } 
     } 

     string search = "Task\r\nTo do: " + oldData[0].ToString() + "\r\nDateTime: " + (DateTime)oldData[1] + "\r\n"; 
     reminders = (Dictionary<DateTime, bool>) oldData[2]; 
     if(reminders.Count != 0) 
     { 
      search += "Reminders\r\n"; 

      foreach (KeyValuePair<DateTime, bool> rem in reminders) 
      { 
       if (rem.Value) 
        search += "speak " + rem.Key + "\r\n"; 
       else 
        search += rem.Key + "\r\n"; 
      } 
     } 

     // get new data 
     string newRemarks = (string)newData[0]; 
     DateTime newDateTime = (DateTime)newData[1]; 
     Dictionary<DateTime, bool> newReminders = (Dictionary<DateTime, bool>)newData[2]; 

     string replace = "Task\r\nTo do: " + newRemarks + "\r\nDateTime: " + newDateTime + "\r\n"; 
     if(newReminders.Count != 0) 
     { 
      replace += "Reminders\r\n"; 
      foreach (KeyValuePair<DateTime, bool> rem in newReminders) 
      { 
       if (rem.Value) 
        replace += "speak " + rem.Key + "\r\n"; 
       else 
        replace += rem.Key + "\r\n"; 
      } 
     } 

     Replace(search, replace); 

     if (index != -1) 
     { 
      remarks = newRemarks; 
      datetime = newDateTime; 
      reminders = newReminders; 
      agenda[index] = this; 
     } 
    } 

替代方法

private void Replace(string search, string replace) 
    { 
     StreamReader reader = new StreamReader(path); 
     string content = reader.ReadToEnd(); 
     reader.Close(); 

     content = Regex.Replace(content, search, replace); 
     content.Trim(); 

     StreamWriter writer = new StreamWriter(path); 
     writer.Write(content); 
     writer.Close(); 
    } 

當在調試運行我得到正確的信息:

content "-- agenda --\r\n\r\nTask\r\nTo do: test\r\nDateTime: 16-4-2012 15:00:00\r\nReminders:\r\nspeak 16-4-2012 13:00:00\r\n16-4-2012 13:30:00\r\n\r\nTask\r\nTo do: testing\r\nDateTime: 16-4-2012 9:00:00\r\nReminders:\r\nspeak 16-4-2012 8:00:00\r\n\r\nTask\r\nTo do: aaargh\r\nDateTime: 18-4-2012 12:00:00\r\nReminders:\r\n18-4-2012 11:00:00\r\n" string 

    search "Task\r\nTo do: aaargh\r\nDateTime: 18-4-2012 12:00:00\r\nReminders\r\n18-4-2012 11:00:00\r\n" string 

    replace "Task\r\nTo do: aaargh\r\nDateTime: 18-4-2012 13:00:00\r\nReminders\r\n18-4-2012 11:00:00\r\n" string 

但事實並非如此改變文字。我如何確保Regex.Replace找到正確的內容?

PS。我確實在這方面檢查了幾個主題,但沒有提到的解決方案適用於我。

回答

0

您錯過了:之後Reminders。只需再次檢查它:)

+0

找到真正的原因,更新回答 – levanovd 2012-04-18 11:08:41

+0

非常感謝。我可以永遠盯着那個,看不到它...... – 2012-04-18 11:15:35

0

你可以嘗試使用StringBuilder建立你想寫出來的文件。

只是在控制檯應用程序中敲了一個快速示例,但這似乎適用於我,我認爲這可能是您正在尋找的。

 StringBuilder sb = new StringBuilder(); 

     sb.Append("Tasks\r\n"); 
     sb.Append("\r\n"); 
     sb.Append("\tTask 1 details"); 

     Console.WriteLine(sb.ToString()); 

     StreamWriter writer = new StreamWriter("Tasks.txt"); 
     writer.Write(sb.ToString()); 
     writer.Close(); 
+0

替換字符串是正確的,可能只是如果它可以在內容中找到搜索字符串 – 2012-04-18 10:45:59