I am trying to highlight an specific word from a file and show all the text in the console with the highlighted word.
I have tried to optimize it with Regular Expressions but got stuck when trying to color red just the desired match in each sentence it appears. So I ended using the For Loop alternative instead.
Is there a better way to do this?
StreamReader sr = new StreamReader("TestFile.txt");
string text = sr.ReadToEnd();
var word = text.Split(" ");
for (int i = 0; i < word.Length; i++)
{
if (word[i].Contains("World", StringComparison.CurrentCultureIgnoreCase))
{
Console.ForegroundColor = ConsoleColor.Red;
Console.Write(word[i] + " ");
Console.ResetColor();
}
else
{
Console.Write(word[i] + " ");
}
}
Console.ReadLine();