-
Notifications
You must be signed in to change notification settings - Fork 4
/
ConvertHTMLTag.cs
83 lines (78 loc) · 2.47 KB
/
ConvertHTMLTag.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
* Write a program that replaces in a HTML document given as string
* all the tags <a href="…">…</a> with corresponding tags [URL=…]…/URL].
* Sample HTML fragment:
* <p>Please visit <a href="http://academy.telerik. com">our site</a> to choose a training course.
* Also visit <a href="www.devbg.org">our forum</a> to discuss the courses.</p>
*
* <p>Please visit [URL=http://academy.telerik. com]our site[/URL] to choose a training course.
* Also visit [URL=www.devbg.org]our forum[/URL] to discuss the courses.</p>
*/
using System;
using System.Collections.Generic;
using System.IO;
class ConvertHTMLTag
{
static void Main(string[] args)
{
try
{
Console.Write("Path:");
string path = @Console.ReadLine();
List<string> htmlFile = new List<string>();
StreamReader reader = new StreamReader(@path);
using (reader)
{
string line = reader.ReadLine();
while (line != null)
{
htmlFile.Add(line);
line = reader.ReadLine();
}
}
for (int i = 0; i < htmlFile.Count; i++)
{
if (htmlFile[i].Contains("<a href=\""))
{
htmlFile[i] = htmlFile[i].Replace("<a href=\"", "[URL=");
}
if (htmlFile[i].Contains("\">"))
{
htmlFile[i] = htmlFile[i].Replace("\">", "]");
}
if (htmlFile[i].Contains("</a>"))
{
htmlFile[i] = htmlFile[i].Replace("</a>", "[/URL]");
}
}
StreamWriter writer = new StreamWriter(@path);
using (writer)
{
foreach (var item in htmlFile)
{
writer.WriteLine(item);
}
}
}
catch (ArgumentException ae)
{
Console.Error.WriteLine(ae.Message);
}
catch (FormatException fe)
{
Console.Error.WriteLine(fe.Message);
}
catch (FileLoadException fle)
{
Console.Error.WriteLine(fle.Message);
}
catch (FileNotFoundException fnfe)
{
Console.Error.WriteLine(fnfe.Message);
}
catch (AccessViolationException ave)
{
Console.Error.WriteLine(ave.Message);
}
}
}