-2

我有以下代码用于将包含字母数字字符的字符串转换为可以使用电话键盘输入电话的数字字符串:

string text = "sometext containing spaces and numbers 12345";
Dictionary<char, char> alphabetMapping = new Dictionary<char, char>();
alphabetMapping['a'] = '2';
alphabetMapping['b'] = '2';
alphabetMapping['c'] = '2';
alphabetMapping['d'] = '3';
alphabetMapping['e'] = '3';
alphabetMapping['f'] = '3';
alphabetMapping['g'] = '4';
alphabetMapping['h'] = '4';
alphabetMapping['i'] = '4';
alphabetMapping['j'] = '5';
alphabetMapping['k'] = '5';
alphabetMapping['l'] = '5';
alphabetMapping['m'] = '6';
alphabetMapping['n'] = '6';
alphabetMapping['o'] = '6';
alphabetMapping['p'] = '7';
alphabetMapping['q'] = '7';
alphabetMapping['r'] = '7';
alphabetMapping['s'] = '7';
alphabetMapping['t'] = '8';
alphabetMapping['u'] = '8';
alphabetMapping['v'] = '8';
alphabetMapping['w'] = '9';
alphabetMapping['x'] = '9';
alphabetMapping['y'] = '9';
alphabetMapping['z'] = '9';
alphabetMapping['1'] = '1';
alphabetMapping['2'] = '2';
alphabetMapping['3'] = '3';
alphabetMapping['4'] = '4';
alphabetMapping['5'] = '5';
alphabetMapping['6'] = '6';
alphabetMapping['7'] = '7';
alphabetMapping['8'] = '8';
alphabetMapping['9'] = '9';
alphabetMapping['0'] = '0';
alphabetMapping['.'] = '0';
alphabetMapping[','] = '0';
alphabetMapping['@'] = '0';
alphabetMapping['*'] = '*';
alphabetMapping['#'] = '#';
alphabetMapping[' '] = '0'; // not sure if this underscore or space

StringBuilder sb = new StringBuilder();
foreach (var c in text)
{
    sb.Append(alphabetMapping[c]);
}

Console.WriteLine(sb.ToString());

输出: 76638398026682464640772237026306862377012345

并不是说这有太多实际用途,而是一些自动电话系统要求输入可能包含字母的密码,所以我编写了这段代码来帮助解决这个问题。

有没有更有效的方法或者我在正确的轨道上?

4

3 回答 3

2

将电话字母转换为各自数字的一种优雅方法是使用正则表达式。这是我使用的功能。可能需要对其进行调整以适应您的情况。

internal static string ConvertPhoneTexttoNumber(string PhoneNumber)
{
    PhoneNumber = Regex.Replace(PhoneNumber, @"[abcABC]", "2");
    PhoneNumber = Regex.Replace(PhoneNumber, @"[defDEF]", "3");
    PhoneNumber = Regex.Replace(PhoneNumber, @"[ghiGHI]", "4");
    PhoneNumber = Regex.Replace(PhoneNumber, @"[jklJKL]", "5");
    PhoneNumber = Regex.Replace(PhoneNumber, @"[mnoMNO]", "6");
    PhoneNumber = Regex.Replace(PhoneNumber, @"[pgrsPGRS]", "7");
    PhoneNumber = Regex.Replace(PhoneNumber, @"[tuvTUV]", "8");
    PhoneNumber = Regex.Replace(PhoneNumber, @"[wxyzWXYZ]", "9");

    //remove all non alphanumeric characters
    PhoneNumber = Regex.Replace(PhoneNumber, @"[\W]", "");             

    return PhoneNumber;
 }

此解决方案适用于字符串中的任意数量的字符。如果要保留括号和破折号,请删除此行:

PhoneNumber = Regex.Replace(PhoneNumber, @"[\W]", "");

注意:如果您不知道如何使用正则表达式,这里有一个很棒的教程: https ://regexone.com/lesson/introduction_abcs

于 2017-02-03T21:54:50.420 回答
1

如果将所有项目组合在一起,则可以使用更少的代码行。

var allValues = new List<KeyValuePair<string,char>>()

allValues.Add(new KeyValuePair("abc2","2"));
allValues.Add(new KeyValuePair("def3","3"));
etc, etc

StringBuilder sb = new StringBuilder();
foreach (var c in text)
{
    var matched = allValues.FirstOrDefault(kvp=> kvp.Key.Contains(c));
    if(matched != null)
    {
       sb.Append(matched.Value);
    }
    else
    {
       sb.Append(" ");
    }
}

Console.WriteLine(sb.ToString());

它的计算效率可能不是更高,但人类阅读的效率更低。

于 2013-10-02T19:04:08.923 回答
1

考虑到数据集的大小(字母数字字符和字母)和预期的应用,这似乎是最合乎逻辑且看似最佳的方法。如果您想提高性能,您可以使用 HashSet,但我不确定您是否会看到使用此数据集的任何可衡量的收益。

于 2013-10-02T18:51:28.827 回答