-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPhoneTranslator.cs
More file actions
43 lines (39 loc) · 1.23 KB
/
Copy pathPhoneTranslator.cs
File metadata and controls
43 lines (39 loc) · 1.23 KB
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
using System.Text;
namespace Core
{
public static class PhonewordTranslator
{
public static string ToNumber(string raw)
{
if (string.IsNullOrWhiteSpace(raw)) return "";
raw = raw.ToUpperInvariant();
var newNumber = new StringBuilder();
foreach (var c in raw)
{
if (" -0123456789".Contains(c)) newNumber.Append(c);
else
{
var result = TranslateToNumber(c);
if (result != null) newNumber.Append(result);
}
}
return newNumber.ToString();
}
private static bool Contains(this string keyString, char c)
{
return keyString.IndexOf(c) >= 0;
}
private static int? TranslateToNumber(char c)
{
if ("ABC".Contains(c)) return 2;
if ("DEF".Contains(c)) return 3;
if ("GHI".Contains(c)) return 4;
if ("JKL".Contains(c)) return 5;
if ("MNO".Contains(c)) return 6;
if ("PQRS".Contains(c)) return 7;
if ("TUV".Contains(c)) return 8;
if ("WXYZ".Contains(c)) return 9;
return null;
}
}
}