Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在给定字符串中查找关键字。最简单、最快的&x2B;到目前为止我所拥有的_C#_Winforms_Richtextbox - Fatal编程技术网

C# 在给定字符串中查找关键字。最简单、最快的&x2B;到目前为止我所拥有的

C# 在给定字符串中查找关键字。最简单、最快的&x2B;到目前为止我所拥有的,c#,winforms,richtextbox,C#,Winforms,Richtextbox,我正在制作一个语法着色工具。我目前正在编写查找和突出显示关键字的方法,例如if-then-else。。我相信有更好(更快、更美观)的方法来实现这一点 下面是两种方法,第一种是我尝试,除了长度之外,不使用任何字符串方法来提高速度 第二次我使用了字符串方法,但有人告诉我,它们比第一次慢 哪条路更快?对于第一个词,只有当该词后面有空格时,该词才会突出显示,这是不对的,有什么补救办法吗 代码: private string[] m_keywords = new string[] { "GOTO"

我正在制作一个语法着色工具。我目前正在编写查找和突出显示关键字的方法,例如if-then-else。。我相信有更好(更快、更美观)的方法来实现这一点

下面是两种方法,第一种是我尝试,除了长度之外,不使用任何字符串方法来提高速度

第二次我使用了字符串方法,但有人告诉我,它们比第一次慢

哪条路更快?对于第一个词,只有当该词后面有空格时,该词才会突出显示,这是不对的,有什么补救办法吗

代码:

    private string[] m_keywords = new string[] { "GOTO", "IF", "THEN", "ELSE", "WHILE", "DO" };
    private int m_nShortestKeywordLength = 2;

    // lcpy_strLine is a copy in all uppercase of the current line I am processing

    private void ProcessKeywords(Color clr)
    {
        if(lcpy_strLine.Length > m_nShortestKeywordLength)
            for (int i = 0; i < m_keywords.Length; i++)
            {
                string curWord = m_keywords[i];
                int len = curWord.Length;
                for (int j = 0; j < lcpy_strLine.Length; j++)
                {
                    if (j + len < lcpy_strLine.Length)
                    {
                        int k = 0;
                        while (k < len && lcpy_strLine[j + k] == curWord[k])
                            k++;
                        if (k == len)
                        {
                            Console.WriteLine("Found Keyword");
                            SelectionStart = m_nLineStart + j;
                            SelectionLength = k;
                            SelectionColor = clr;
                        }
                    }
                }
            }
    }

    private void ProcessKeywords2(Color clr)
    {
        /*for (int i = 0; i < m_keywords.Length; i++)
            if (lcpy_strLine.Contains(m_keywords[i]))
            {
                int indx1 = lcpy_strLine.IndexOf(m_keywords[i]);
                SelectionStart = m_nLineStart + indx1;
                SelectionLength = m_keywords[i].Length;
                SelectionColor = clr;
            }*/

    }
private string[]m_keywords=新字符串[]{“GOTO”、“IF”、“THEN”、“ELSE”、“WHILE”、“DO”};
private int m_nShortestKeywordLength=2;
//lcpy_strLine是我正在处理的当前行的全大写副本
私有void进程关键字(颜色clr)
{
如果(lcpy_strLine.Length>m_nShortestKeywordLength)
对于(int i=0;i
使用
string.IndexOf
时,需要指定
StringComparison.Ordinal
,以获得良好的性能。
默认重载使用区域性感知比较(例如,它认为“æ”等于“ae”),这比简单的逐字符比较要昂贵得多。

使用
string.IndexOf
时,需要指定
StringComparison.Ordinal
,以获得良好的性能。
默认重载使用区域性感知比较(例如,它认为“æ”等于“ae”),这比简单的逐字符比较要昂贵得多。

最简单的方法可能是使用正则表达式。它也会相当快

private string[] m_keywords = new string[] { "GOTO", "IF", "THEN", "ELSE", "WHILE", "DO" };
private Regex keywordRegex = new Regex(@"\b(" + string.Join("|", m_keywords) + @")\b", RegexOptions.Compiled | RegexOptions.IgnoreCase);
无需将行大写:

private void ProcessKeywords(Color clr)
{
    foreach (Match m in keywordRegex.Matches(someLine)) {
        SelectionStart = m.Index;
        SelectionLength = m.Length;
        SelectionColor = clr;
    }
}

最简单的方法可能是使用正则表达式。它也会相当快

private string[] m_keywords = new string[] { "GOTO", "IF", "THEN", "ELSE", "WHILE", "DO" };
private Regex keywordRegex = new Regex(@"\b(" + string.Join("|", m_keywords) + @")\b", RegexOptions.Compiled | RegexOptions.IgnoreCase);
无需将行大写:

private void ProcessKeywords(Color clr)
{
    foreach (Match m in keywordRegex.Matches(someLine)) {
        SelectionStart = m.Index;
        SelectionLength = m.Length;
        SelectionColor = clr;
    }
}

我会使用正则表达式,例如
(GOTO | IF | THEN | ELSE |…)
不要过早地优化代码。以最具可读性和可维护性的方式编写代码,如果性能有问题,请稍后重新查看代码。但在任何情况下,
processKeywords 2
的搜索量是需要的两倍<如果字符串不包含关键字,则code>IndexOf返回
-1
,因此请检查该字符串并对
indx1
使用相同的结果。我会使用正则表达式,例如
(GOTO | if | THEN | ELSE |…)
不要过早地优化代码。以最具可读性和可维护性的方式编写代码,如果性能有问题,请稍后重新查看代码。但在任何情况下,
processKeywords 2
的搜索量是需要的两倍<如果字符串不包含关键字,则code>IndexOf返回
-1
,因此请检查该字符串,并对
indx1
使用相同的结果。