Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8414583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T01:12:14+00:00 2026-06-10T01:12:14+00:00

I am making a syntax coloring tool. I am currently writing the method to

  • 0

I am making a syntax coloring tool. I am currently writing the method to find and highlight the keywords, ex if then else.. I am sure there is a better (faster and more aesthetic) way to do this.

Below are two methods, the first I try and not to use any string methods except for length to try and improve speed.

The second I used string methods but I’ve been told they are slower then doing it the first way.

Which way is faster? And for the first one, the word is only highlighted when a space is after that word which is not right, any remedy for that too?

Code:

    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;
            }*/

    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T01:12:15+00:00Added an answer on June 10, 2026 at 1:12 am

    The easiest way to do this would probably be a regular expression. It’ll be reasonably fast, too.

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

    And no need to uppercase the line:

    private void ProcessKeywords(Color clr)
    {
        foreach (Match m in keywordRegex.Matches(someLine)) {
            SelectionStart = m.Index;
            SelectionLength = m.Length;
            SelectionColor = clr;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing the wrong syntax, so the function is not getting called. I'm making
I'm making a Brainfuck (programming language) IDE, and I'm stuck with the syntax coloring.
I'm sure this is just some stupid syntax error I'm making, but when my
I am currently working on syntax highlighting and code completion project, and making user
Making UML sequence diagram in VS 2010RC I've observed that there is no activation
Making websites that appear correctly in IE is a big problem. Is there any
I know I must be making a simple syntax mistake, but I want to
Please don't mind about the syntax. I thought of making the post as brief
I want to create a good syntax for making it easier to link to
I'm making a little program in Prolog to find out the capitals of a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.