Дано некоторое конечное мн-во слов. Исключить их из текстового файла - C#
Формулировка задачи:
Дано некоторое конечное мн-во слов. Исключить их из текстового файла. Например, из файла с текстом программы исключить все слова Begin, End.
Помогите пожалуйста, тема актуальна
Решение задачи: «Дано некоторое конечное мн-во слов. Исключить их из текстового файла»
textual
Листинг программы
class Program
{
static void Main(string[] args)
{
string path = "1.txt";
string[] clearText = {"Sasha", "Fast", "azaza"};
Write(path,Clear(ReadFile(path),clearText));
Console.ReadKey();
}
private static string ReadFile(string path)
{
string text = "";
try
{
using (StreamReader streamReader = new StreamReader(path, Encoding.GetEncoding(1251)))
{
text = streamReader.ReadToEnd();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return text;
}
private static string Clear(string text, string[] textClear)
{
foreach (var s in textClear)
{
if(text.Contains(s))
text = text.Replace(s, "");
}
return text;
}
private static void Write(string path, string text)
{
try
{
using (StreamWriter streamWriter = new StreamWriter(path,false))
{
streamWriter.Write(text);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}