/
c-sharp.cs
47 lines (43 loc) · 1.02 KB
/
c-sharp.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
using System;
using System.Collections.Generic;
using System.Text;
namespace asciify
{
static class Asciify
{
public static void Main (string[] args)
{
Console.WriteLine (asciify("Héllöþ☺"));
}
private static Dictionary<char,String> specialCases = new Dictionary<char,String> () {
{'æ', "ae"},
{'Æ', "AE"},
{'œ', "oe"},
{'Œ', "OE"},
{'þ', "th"},
{'Þ', "TH"},
{'ä', "ae"},
{'Ä', "AE"},
{'ö', "oe"},
{'Ö', "OE"},
{'ü', "ue"},
{'Ü', "UE"},
{'ß', "ss"},
};
public static String asciify(String text) {
text = text.Normalize (System.Text.NormalizationForm.FormC);
StringBuilder newText = new StringBuilder ();
foreach (char c in text) {
newText.Append (specialCases.ContainsKey(c) ? specialCases[c] : c.ToString ());
}
text = newText.ToString().Normalize (System.Text.NormalizationForm.FormD);
newText = new StringBuilder ();
foreach (char c in text) {
if (c <= 127) {
newText.Append (c);
}
}
return newText.ToString ();
}
}
}