title: C# - split a RichTextBox line in two based on the caret position
body: <p>I've got a RichTextBox, here referred to as <code>box</code>.</p>
  
  <pre><code>string currentline = box.Lines[box.GetLineFromCharIndex(box.SelectionStart)];
  </code></pre>
  
  <p>That line there fetches the line the caret is in. It works excellently.</p>
  
  <p>However, I have a need to get two strings from this. The first is everything on that line UP to the caret, and the second is everything on that line AFTER it.</p>
  
  <p>For instance, if the line is <code>How is you|r day going?</code>, with <code>|</code> representing the caret, I would get <code>How is you</code> and <code>r day going?</code>, separately.</p>
  
  <p>I wrote this monstrosity, which works:</p>
  
  <pre><code>string allbefore = box.Text.Substring(0, box.SelectionStart);
  string allafter = box.Text.Substring(box.SelectionStart, box.Text.Length - box.SelectionStart);
  string linebefore = "";
  for (int i = 0; i &lt; allbefore.Length; i++)
  {
      linebefore += allbefore[i];
      if (allbefore[i] == '\n')
          linebefore = "";
  }
  string lineafter = "";
  for (int i = 0; i &lt; allafter.Length; i++)
  {
      if (allafter[i] == '\n')
          break;
      else
          lineafter += allafter[i];
  }
  </code></pre>
  
  <p>It gives me the result I want, but involves looping through EVERY character in the entire box, which just hurts. Is there an easy way to do this I'm just missing? Thanks.</p>