title: Replace the last occurrence of a word in a string - C#
body: <p>I have a problem where I need to replace the last occurrence of a word in a string.</p>
  
  <p><strong>Situation:</strong> I am given a string which is in this format:</p>
  
  <pre><code>string filePath ="F:/jan11/MFrame/Templates/feb11";
  </code></pre>
  
  <p>I then replace <code>TnaName</code> like this:</p>
  
  <pre><code>filePath = filePath.Replace(TnaName, ""); // feb11 is TnaName
  </code></pre>
  
  <p>This works, but I have a problem when <code>TnaName</code> is the same as my <code>folder name</code>. When this happens I end up getting a string like this:</p>
  
  <pre><code>F:/feb11/MFrame/Templates/feb11
  </code></pre>
  
  <p>Now it has replaced both occurrences of <code>TnaName</code> with <code>feb11</code>. Is there a way that I can replace only the last occurrence of the word in my string?</p>
  
  <p><strong>Note: <code>feb11</code> is <code>TnaName</code> which comes from another process - that's not a problem.</strong></p>