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:
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);
}
}
My problem is that if I try to replace a string that looks like this: "partnumber: 58" and there also is a string that looks like this "partnumber: 585"
My problem is that if the current string contains the desired substring and in addition a string that has a high similarity like "partnumber: 58" and "partnumber: 585", my code will also replace the highly similar string.
Is there a way I can avoid this behavoir?