如何转换的字母数字的电话号码位数 [英] How to convert an alphanumeric phone number to digits

查看:172
本文介绍了如何转换的字母数字的电话号码位数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:

我的实用程序的最终版本是这样的:

The final version of my utility looks like this:

StringBuilder b = new StringBuilder();

for(char c : inLetters.toLowerCase().toCharArray())
{
	switch(c)
	{
	case '0':                                          b.append("0"); break;
	case '1':                                          b.append("1"); break;
	case '2': case 'a': case 'b': case 'c':            b.append("2"); break;
	case '3': case 'd': case 'e': case 'f':            b.append("3"); break;
	case '4': case 'g': case 'h': case 'i':            b.append("4"); break;
	case '5': case 'j': case 'k': case 'l':            b.append("5"); break;
	case '6': case 'm': case 'n': case 'o':            b.append("6"); break;
	case '7': case 'p': case 'q': case 'r': case 's':  b.append("7"); break;
	case '8': case 't': case 'u': case 'v':            b.append("8"); break;
	case '9': case 'w': case 'x': case 'y': case 'z':  b.append("9"); break;
	}
}

return builder.toString();



原题:

我是在转换上的字母数字的电话号码,以一串数字的简单的任务。例如,1-800-HI-HAXOR将成为1-800-44-42967。我最初的尝试是建立一个讨厌的switch语句,但我喜欢一个更优雅和高效的解决方案。以下是我有:

I'm taking on the simple task of converting an alphanumeric phone number to a string of digits. For example, 1-800-HI-HAXOR would become 1-800-44-42967. My initial attempt was to create a nasty switch statement, but I'd love a more elegant, and efficient solution. Here's what I've got:

for(char c : inLetters.toLowerCase().toCharArray())
{
    switch(c)
    {
    case '0':                                         result+="0"; break;
    case '1':                                         result+="1"; break;
    case '2': case 'a': case 'b': case 'c':           result+="2"; break;
    case '3': case 'd': case 'e': case 'f':           result+="3"; break;
    case '4': case 'g': case 'h': case 'i':           result+="4"; break;
    case '5': case 'j': case 'k': case 'l':           result+="5"; break;
    case '6': case 'm': case 'n': case 'o':           result+="6"; break;
    case '7': case 'p': case 'q': case 'r': case 's': result+="7"; break;
    case '8': case 't': case 'u': case 'v':           result+="8"; break;
    case '9': case 'w': case 'x': case 'y': case 'z': result+="9"; break;
    }
}

谢谢!

推荐答案

switch语句是不是真的那么坏。你的算法是线性相对于的电话号码的长度。在code是可读和pretty的易核实检查。我不会惹它,除了增加一个默认情况下处理错误。 (我不是一个Java程序员,所以请原谅我,如果它被称为别的东西。)

The switch statement is not really that bad. Your algorithm is linear with respect to the length of the phone number. The code is readable and pretty easy to verify by inspection. I wouldn't mess with it, except to add a default case for handling errors. (I'm not a Java programmer, so forgive me if it's called something else.)

如果您的有无的,使其速度更快,通过字索引的pre-初始化表将可以避免超越了基本的错误检查任何比较。你甚至可以通过在表中重复的值避免的情况下转换(数字['A'] =数字['一'] =2; )。初始化表的成本将被分摊在转换总数。

If you have to make it faster, a pre-initialized table indexed by character would avoid any comparisons beyond basic error checking. You could even avoid the case conversion by duplicating the values in the table (digit['A'] = digit['a'] = "2";). The cost of initializing the table would be amortized over the total number of conversions.

这篇关于如何转换的字母数字的电话号码位数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆