title: Replacing part of text in richtextbox
body: <p>I need to compare a value in a string to what user typed in a richtextbox. </p>
  
  <p>For example: if a richtextbox holds <code>string rtbText = "aaaka"</code> and I compare this to another variable <code>string comparable = "ka"</code>(I want it to compare backwards). I want the last 2 letters from <code>rtbText</code> (comparable has only 2 letters) to be replaced with something that was predetermined(doesn't really matter what). 
  So <code>rtbText</code> should look like this:</p>
  
  <p><code> rtbText = "aaa(something)"</code></p>
  
  <p>This doesn't really have to be compared it can just count letters in <code>comparable</code> and based on that it can remove 2 letters from <code>rtbText</code> and replace them with something else.</p>
  
  <p>UPDATE:
  Here is what I have:</p>
  
  <pre><code>int coLen = comparable.Length;
             comparable = null;
             TextPointer caretBack = rtb.CaretPosition.GetPositionAtOffset(coLen, LogicalDirection.Backward);
             TextRange rtbText = new TextRange(rtb.CaretPosition, caretBack);
             string text = rtbText.Text;
  </code></pre>
  
  <p>rtbText returns an empty string or I get an error for everything longer than 3 characters. What am I doing wrong?    </p>
  
  <p>Let me elaborate it a little bit further. I have a listbox that holds replacements for values that user types in rtb. The values(replacements) are coming from there, meaning that I don't really need to go through the whole text to check values. I just need to check the values right before caret. I am comparing these values to what I have stored in another variable (<code>comparable</code>). <img src="https://i.stack.imgur.com/Cma6Z.jpg" alt="What needs to be done"> </p>
  
  <p>Please let me know if you don't understand something. 
  I did my best to explain what needs to be done.
  Thank you</p>