title: I need a faster way to highlight text in a RichTextBox
body: <p>I have this project to highlight the common statements in two files.<br>
  So I read each of them in RichTextBox and find the common statements and put them into a string, named <code>output</code> here.<br>
  Now what I need is a faster way to highlight only the common statements in both RichTextBoxes.  </p>
  
  <p>Here is my code:</p>
  
  <pre><code>string[] splitted = output.Split('.');
  
  int index1;
  
  for (int i = 0; i &lt; splitted.Length; i++)
  {
     index1 = 0;
     while (index1 &lt; rtb1.Text.LastIndexOf(splitted[i]) + 1)
     {
        rtb1.Find(splitted[i], index1, rtb1.TextLength, RichTextBoxFinds.None);
        rtb.SelectionBackColor = Color.Yellow;
        index1 = rtb1.Text.IndexOf(splitted[i], index1) + 1;
     }
  }
  
  for (int i = 0; i &lt; splitted.Length; i++)
  {
     index1 = 0;
     while (index1 &lt; rtb2.Text.LastIndexOf(splitted[i]) + 1)
     {
        rtb2.Find(splitted[i], index1, rtb2.TextLength, RichTextBoxFinds.None);
        rtb.SelectionBackColor = Color.Yellow;
        index1 = rtb.Text.IndexOf(splitted[i], index1) + 1;
     }
  }
  </code></pre>
  
  <p>Where <code>rtb1</code> and <code>rtb2</code> are the RichTextBoxes.<br>
  I got the highlighting code from another thread here. Now what I believe that this code takes O(N^3) or even more.<br>
  I need to optimize it to O(N^2) at most. I'd appreciate any Help.</p>
  
  <p>Edit:<br>
  I also tried using <code>Regex.Matches</code>:</p>
  
  <pre><code>Regex reg = new Regex(output, RegexOptions.Compiled);
  MatchCollection matches = reg.Matches(rtb1.Text);
  
  foreach (Match match in matches)
  {
     rtb1.Select(match.Index, match.Length);
     rtb1.SelectionBackColor = Color.Yellow;
  }
  </code></pre>
  
  <p>When <code>rtb1</code> contains:</p>
  
  <blockquote>
    <p>[1] INVESTIGATING THE IMPACT OF THE YEAR 2000 PROBLEM  The Year 2000 (Y2K) technology problem started as an innocuous short-term solution to the oppressively high cost of computer memory in the 1950s and 1960s. 
    [2] Programmers expected that the problems created by the limited, two-digit method of date storage would solve themselves as companies, governments and other computer-owners updated their hardware and software. 
    [3] Fifty years after the introduction of the computer, the Y2K problem has the potential to develop into a worldwide crisis.</p>
  </blockquote>
  
  <p>And <code>rtb2</code> contains:</p>
  
  <blockquote>
    <p>[1] INVESTIGATING THE IMPACT OF THE YEAR 2000 PROBLEM  The Year 2000 (Y2K) technology problem started as an innocuous short-term solution to the oppressively high cost of computer memory in the 1950s and 1960s. 
    [3] Fifty years after the introduction of the computer, the Y2K problem has the potential to develop into a worldwide crisis. </p>
  </blockquote>
  
  <p><code>output</code> should contain:</p>
  
  <blockquote>
    <p>[1] INVESTIGATING THE IMPACT OF THE YEAR 2000 PROBLEM  The Year 2000 (Y2K) technology problem started as an innocuous short-term solution to the oppressively high cost of computer memory in the 1950s and 1960s. 
    [3] Fifty years after the introduction of the computer, the Y2K problem has the potential to develop into a worldwide crisis.</p>
  </blockquote>