title: Replacing words in a string with values from Dictionary in c#
body: <p>I have a simple <code>dictionary</code> like this:</p>
  
  <p><code>var fruitDictionary = new Dictionary&lt;string, string&gt; {Apple,Fruit}, {Orange, Fruit}, {Spinach, Greens}</code></p>
  
  <p>and I have a string like </p>
  
  <p><code>var fruitString = Apple Orange Spinach Orange Apple Spinach</code></p>
  
  <p>How to replace all occurrences of the particular word in that sentence with the <code>matching-word</code> from the dictionary?</p>
  
  <p>(i.e.) The above sentence should read <code>Fruit Fruit Greens Fruit Fruit Fruit</code> ?</p>
  
  <p>Any ideas is much appreciated.</p>
  
  <p>EDIT: </p>
  
  <p>I tried something like this:</p>
  
  <pre><code>var outputString = string.Empty;
  fruitString.ToArray().ToList().Foreach(item =&gt;
  {
  if (fruitDictionary.ContainsKey(item))
  {
   outputString = outputString + fruitDictionary[item];
  
  } 
  </code></pre>
  
  <p>Any optimal solution for this? The above code is not optimal because, it does <code>traversing</code> the entire-length of given array!</p>