title: best practice for string replacement in c#
body: <p>I just wrote a console application to to replace a certain string within a large number of utf-8 coded files. I need to cover about 20 different cases of this string so I reduced my codesnippet to the necessary parts.
   The Code looks like this:</p>
  
  <pre><code>foreach (String file in allFIles)
  {
      string text = "";
      using (StreamReader r = new StreamReader(file))
      {
          text = r.ReadToEnd();
      }
  
      if (text.Contains(Case1))
      {
          string textCase1 = "";
          using (StreamReader rCase1Reader = new StreamReader(file))
          {
              textCase1 = rCase1Reader.ReadToEnd().Replace(Case1, Case1Const);
          }
          using (StreamWriter wCase1 = new StreamWriter(file, false, Encoding.UTF8))
          {
              wCase1.Write(textCase1);
          }
  
          UsedFIles.Add(file);
      }
  }
  </code></pre>
  
  <p>My problem is that if I try to replace a string that looks like this: <code>"partnumber: 58"</code> and there also is a string that looks like this <code>"partnumber: 585"</code> </p>
  
  <p>My problem is that if the current string contains the desired substring and in addition a string that has a high similarity like <code>"partnumber: 58"</code> and <code>"partnumber: 585"</code>, my code will also replace the highly similar string.
  Is there a way I can avoid this behavoir?</p>