繁体   English   中英

如何检查richTextBox1中的行在文本文件中是否已经存在?

[英]How to check if a line in richTextBox1 is already exist or not in a text file?

在form1顶部:

private List<string> entriesRlines = new List<string>();
private List<string> entriesLines = new List<string>();
private string[] lines;
private List<string> RedAlerts;
private StreamWriter WriteAlerts;
private string AlertsDirectory;
private string AlertsFilename;
private string combineAlert;

在form1构造函数中:

AlertsDirectory = Path.GetDirectoryName(Application.LocalUserAppDataPath) + @"\alerts";
if (!Directory.Exists(AlertsDirectory))
{
  Directory.CreateDirectory(AlertsDirectory);
}
AlertsFilename = "Alerts.txt";
combineAlert = Path.Combine(AlertsDirectory, AlertsFilename);
if (File.Exists(combineAlert))
            {
                lines = File.ReadAllLines(combineAlert);
                label5.Text = lines.Length.ToString();
                RedAlerts = new List<string>(lines);
            }
            RedColorAlert();

然后是RedColorAlert方法:

private void RedColorAlert()
        {
            WriteAlerts = new StreamWriter(combineAlert,true);
            string[] rlines = richTextBox1.Lines;
            int linespos;
            if (lines != null)
            {
                for (int x = 0; x < lines.Length; x++)
                {
                    linespos = lines[x].IndexOf("===>");
                    string t = lines[x].Substring(linespos + 4);
                    entriesLines.Add(t);
                }
                for (int y = 0; y < rlines.Length; y += 4)
                {
                    entriesRlines.Add(rlines[y]);
                }
                for (int i = 0; i < entriesLines.Count; i++)
                {
                    for (int f = 0; f < entriesRlines.Count; f++)
                    {
                        if (!entriesLines[i].Contains(entriesRlines[f]))
                        {
                            if (entriesRlines[i].Contains("צבע אדום") || entriesRlines[i].Contains("אזעקה"))
                            {
                                timer3.Start();
                                WriteAlerts.WriteLine(DateTime.Now + "   ===>   " + entriesRlines[i]);
                                break;
                            }
                        }
                    }
                }
            }
            else
            {
                for (int i = 0; i < rlines.Length; i++)
                {
                    if (rlines[i].Contains("צבע אדום") || rlines[i].Contains("אזעקה"))
                    {
                        timer3.Start();
                        WriteAlerts.WriteLine(DateTime.Now + "   ===>   " + rlines[i]);
                    }
                }
            }
            WriteAlerts.Close();
        }

如果文本文件第一次为空,则第二部分为ELSE部分。 如果文本文件已经存在并且内部有一个或多个行,请执行第一部分:

for (int i = 0; i < entriesLines.Count; i++)
                {
                    for (int f = 0; f < entriesRlines.Count; f++)
                    {
                        if (!entriesLines[i].Contains(entriesRlines[f]))
                        {
                            if (entriesRlines[i].Contains("צבע אדום") || entriesRlines[i].Contains("אזעקה"))
                            {
                                timer3.Start();
                                WriteAlerts.WriteLine(DateTime.Now + "   ===>   " + entriesRlines[i]);
                                break;
                            }
                        }
                    }
                }

由于某些原因,此循环循环一直无法正常运行,即使该行已经在文本文件中,也始终进入内部并启动timer3和WriteLine。 我使用了一个断点。

这是文本文件现在的样子:

2014年8月2日8:11:27 PM ===> 18:46צבעאדוםבמ״אאשכול--נפילה。 א3רחנפצע,מפונהבמסו8/3/2014 4:25:08 AM ===> 04:13צבעאדוםבמ״אשדותנגבושערהנגב2014/8/3 4:25:51 AM ====> 04:13צבע 8/3/2014 4:26:50 AM ===> 04:13צבצא״ום במ״אשדותנגבושערהנגב

第二行应该像第三行和第四行一样,但是由于某种原因第二行与第一行混合在一起。 第三和第四行应该与第二行相同,并且永远不要被写入。

想法是,如果还包含我所说的条件的行在文本文件中不存在,则在文本文件中添加新行。 如果一行已经存在,请不要启动timer3,也不要再次写入它。

这是文本文件以及entryLines中行的外观:

2014年8月3日4:26:50 ===> 04:13צבעאדוםבמ״אשדותנגבושערהנגב

这是一条不在文本文件中的行,或者看起来确实像在richTextFile1以及entryRlines中一样:

04:36צבעאדוםבמ״אשדותנגבושערהנגב

entryRlines是List,在psre之后包含richTextBox1的行。 entryLines是List,其中包含来自richTextBox1的行。

例如,某些希伯来语字符串应从右到左显示

א

从右到左: http : //www.fileformat.info/info/unicode/char/5d0/index.htm

(而且Firefox根本无法正确处理它!这就是为什么我不得不将其放在单独的行中。)

也许在GUI中显示的字符串不是您想的那样,因为它们是从右到左显示的,但实际上是从左到右存储的? 或相反亦然? 谁知道Visual Studio在显示带有从右到左字符的字符串文字时的作用? 我当然不会...天哪! VS 2008有时似乎是从右到左显示它们! 如果我将光标放在您的字符串文字之一的开头,然后按向左箭头,则它会跳到字符串的结尾,然后向后退!

我建议您使用以下一种实用程序来调试问题,以确保您的字符串按期望的顺序包含期望的字符:

    public static IEnumerable<int> Utf32CodePoints(string s, int index)
    {
        for (int length = s.Length; index < length; index++)
        {
            yield return char.ConvertToUtf32(s, index);
            if (char.IsSurrogatePair(s, index))
                index++;
        }
    }

    public static IEnumerable<KeyValuePair<int, int>> Utf32IndexedCodePoints(string s, int index)
    {
        for (int length = s.Length; index < length; index++)
        {
            yield return new KeyValuePair<int, int>(index, char.ConvertToUtf32(s, index));
            if (char.IsSurrogatePair(s, index))
                index++;
        }
    }

接下来,删除所有这些内联字符串文字(无论如何,字符串文字在代码中都不应出现多次),然后创建常量,例如:

    const string AlertType1 = "צבע אדום";
    const string AlertType2 = "אזעקה";

然后,使用这些实用程序检查常量字符串,TextBox字符串和文件字符串,以查看字符的真正顺序。

顺便说一句,我建议您始终以尽可能小的范围声明字段和变量。 例如,您应该仅在所需范围内定义变量,而不是字段“ WriteAlerts”:

        using (var WriteAlerts = new StreamWriter(AlertsDirectoryAndName, true))
        {
            // your code
        }

更新资料

看起来Visual Studio 2008甚至从我在英语-美国语言环境中,都从右到左显示希伯来语字符串! 我写了以下代码:

    const string Hebrew1 = "צבע אדום";
    const string Hebrew1ApparentFirstChar = "ם";

    static void TestHebrewConstantStringOrder() 
    {
        List<int> Hebrew1Utf32 = new List<int>(TextHelper.Utf32CodePoints(Hebrew1, 0));
        foreach (var codePoint in TextHelper.Utf32CodePoints(Hebrew1ApparentFirstChar, 0))
        {
            int index = Hebrew1Utf32.IndexOf(codePoint);
            Debug.WriteLine(string.Format("Index of U+{0:X8}:  {1}", codePoint, index));
        }
        Debug.WriteLine("Codepoints of " + Hebrew1);
        foreach (var pair in TextHelper.Utf32IndexedCodePoints(Hebrew1, 0))
        {
            string msg = string.Format("{0,7}: U+{1:X8}", pair.Key, pair.Value);
            Debug.WriteLine(msg);
        }
    }

并得到以下输出:

Index of U+000005DD:  7
Codepoints of צבע אדום
      0: U+000005E6
      1: U+000005D1
      2: U+000005E2
      3: U+00000020
      4: U+000005D0
      5: U+000005D3
      6: U+000005D5
      7: U+000005DD
Codepoints of ם
      0: U+000005DD

并且,如果您的浏览器渲染的内容与我的渲染不同(确实有可能!),以下是屏幕截图:

码

输出量

如您所见,看起来在字符串中的第一个“ם”字符实际上是最后一个。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM