How to convert an alphanumeric phone number to digits

前端 未结 7 966
臣服心动
臣服心动 2021-01-05 12:50

UPDATE:

The final version of my utility looks like this:

StringBuilder b = new StringBuilder();

for(char c : inLetters.toLowerCase(         


        
7条回答
  •  粉色の甜心
    2021-01-05 13:27

    If you want a solution that doesn't force you to enumerate all of the letters, you could do something like:

    char convertedChar = c;
    if (Character.isLetter(c)) {
        //lowercase alphabet ASCII codes: 97 (a)-122 (z)
        int charIndex = ((int)c) - 97;
        //make adjustments to account for 's' and 'z'
        if (charIndex >= 115) { //'s'
            charIndex--;
        }
        if (charIndex == 121) { //'z'-1
            charIndex--;
        }
        convertedChar = (char)(2 + (charIndex/3));
    }
    result += convertedChar;
    

提交回复
热议问题