/
12. RemoveWords.cs
35 lines (31 loc) · 1.09 KB
/
12. RemoveWords.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// Write a program that deletes from a text file all words that start with the prefix "test". Words contain only the symbols 0...9, a...z, A…Z, _.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
class DeleteWordsWithPrefixTest
{
//Write a program that deletes from a text file all words that start with the prefix "test".
//Words contain only the symbols 0...9, a...z, A…Z, _.
static void Main(string[] args)
{
StreamReader reader = new StreamReader("testFile.txt");
StreamWriter writer = new StreamWriter("withoutprefixtest.txt");
string line = reader.ReadLine();
using (reader)
{
using (writer)
{
while (line != null)
{
string lineWithoutTest = Regex.Replace(line, @"\btest\w*\b", String.Empty);
writer.WriteLine(lineWithoutTest);
line = reader.ReadLine();
}
}
}
Console.WriteLine("Done!");
}
}