How to convert an alphanumeric phone number to digits

前端 未结 7 991
臣服心动
臣服心动 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:28

    How about simply:

       String convert(String inLetters) {
          String digits = "22233344455566677778889999";
          String alphas = "abcdefghijklmnopqrstuvwxyz";
          String result = "";
          for (char c : inLetters.toLowerCase().toCharArray()) {
              int pos = alphas.indexOf(c);
              result += (pos == -1 ? c : digits.charAt(pos));
          }
          return result;
       }
    

提交回复
热议问题