-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCSV.cs
50 lines (42 loc) · 1.22 KB
/
CSV.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
namespace StreamingServiceCompare
{
// Source: http://stackoverflow.com/questions/4685705/good-csv-writer-for-c#4685745
public static class CSV
{
private const string QUOTE = "\"";
private const string ESCAPED_QUOTE = "\"\"";
private static readonly char[] CHARACTERS_THAT_MUST_BE_QUOTED = new char[] { ';', '"', '\n' };
public static string Escape(string s)
{
if (s == null)
{
return null;
}
if (s.Contains(QUOTE))
{
s = s.Replace(QUOTE, ESCAPED_QUOTE);
}
if (s.IndexOfAny(CHARACTERS_THAT_MUST_BE_QUOTED) > -1)
{
s = QUOTE + s + QUOTE;
}
return s;
}
public static string Unescape(string s)
{
if (s == null)
{
return null;
}
if (s.StartsWith(QUOTE) && s.EndsWith(QUOTE))
{
s = s.Substring(1, s.Length - 2);
if (s.Contains(ESCAPED_QUOTE))
{
s = s.Replace(ESCAPED_QUOTE, QUOTE);
}
}
return s;
}
}
}