it's easy to swap numbers with letters. A mapping table is fine, but do you want to map aquarz
directly with 0such 9
? I admire your brain circuits
< hr >
const DATA = [
'',
'',
'abcABC',
'defDEF',
'ghiGHI',
'jklJKL',
'mnoMNO',
'pqrsPQRS',
'tuvTUV',
'wxyzWXYZ',
];
const getNumber = str => str.split('').map(v => DATA.findIndex(s => s.includes(v))).join('');
this is the 9-grid conversion method written earlier, refer to
getNumber('r')// 7
getNumber('d') //3
java programming converts 26 letters into 09digits in the form of a mobile phone keyboard
this seems to match your needs
you can try to use the ASCII code to judge, but it feels that the array method above is actually more efficient
public static String getNum(String value)
{
StringBuffer sbu = new StringBuffer();
char[] chars = value.toCharArray();
for (int i = 0; i < chars.length; iPP) {
int num = (int)chars[i];
if (num < 97 || num >122){
return "";
}
int index = (num -97)/3;
if (index < 5) {
sbu.append(index+2);
}else if(num < 116){
sbu.append(7);
}else if(num < 119){
sbu.append(8);
}else{
sbu.append(9);
}
}
return sbu.toString();
}