0

我目前有一个文本框字段,每次进行更改时,我都会添加更新者和时间

现在它继续附加该文本

我怎样才能找到写着“LastEdited:”的行,并且只替换时间戳值?

这就是我现在所做的也许有人可以向我发布如何获取时间戳并替换它的代码示例?

if (txtMemberNotesOriginally != txtMemberNotesChanged) {
            txtMemberNotes.AppendText("LastEdited: " + DateTime.Now.ToString() + " By: " + MyProgramName.Username + Environment.NewLine);

我不是专家,所以代码示例会非常有用

我确实使用这个文本框来保存其他笔记(我不想松动)

我可以这样做吗

foreach (var line in txtMemberNotes.Lines) {
                if (line.StartsWith("Last Edited: "))
                {
                    txtMemberNotes.Text = txtMemberNotes.Text.Replace(line, "Last Edited: " + DateTime.Now.ToString() + " By: " + MyProgramName.Username + Environment.NewLine);
                }
                else 
                {
                    txtMemberNotes.AppendText("Last Edited: " + DateTime.Now.ToString() + " By: " + MyProgramName.Username + Environment.NewLine);
                }
            }
4

2 回答 2

1

解决问题的最简单方法似乎是停止附加文本并设置文本值。不需要解析,只需用新值覆盖现有值。

if (txtMemberNotesOriginally != txtMemberNotesChanged) {
            txtMemberNotes.Text = "LastEdited: " + DateTime.Now.ToString() + " By: " + MyProgramName.Username + Environment.NewLine;
于 2012-11-08T04:00:56.413 回答
0

如果你真的想要,我想总是有 Regex:

string s = @"Some notes here etc etc..

              Last edited: 12/12/2012 12:00:00 AM";

Console.Write(Regex.Replace(s, @"Last edited: .*$", "Lasted edited: " + DateTime.Now.ToString()));

或者更具体地说,使用您的代码:

txtMemberNotes.Text = Regex.Replace(txtMemberNotes.Text, @"Last edited: .*$", "Lasted edited: " + DateTime.Now.ToString());
于 2012-11-08T05:02:23.117 回答