在JavaScript中将字母转换为数字

时间:2015-01-10 14:21:24

标签: javascript alphabetical

我想知道如何将输入的每个字母字符转换为数字。

e.g。 a = 1,b = 2,c = 3直到z = 26

在C中我通过获取字符输入并将其显示为整数来设法做类似的事情。但我不确定如何在JavaScript中执行此操作。

8 个答案:

答案 0 :(得分:38)

如果我说得对,提供的答案过于复杂:

parseInt('a', 36) - 9; // 1
parseInt('z', 36) - 9; // 26
parseInt('A', 36) - 9; // 1
parseInt('Z', 36) - 9; // 26
'a'.charCodeAt() - 96; // 1
'z'.charCodeAt() - 96; // 26
'A'.charCodeAt() - 64; // 1
'Z'.charCodeAt() - 64; // 26
function charToNumber (s, i) {
    return parseInt(s.charAt(i), 36) - 9;
}

function sumChars (s) {
    var i = s.length, r = 0;
    while (--i >= 0) r += charToNumber(s, i);
    return r;
}
sumChars('az'); // 27

答案 1 :(得分:8)

var alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"];
var letter = "h";
var letterPosition = alphabet.indexOf(letter)+1;

编辑:

计算字符串内的字母的可能性,aa = 2,ab = 3等。

function str_split(string, split_length) {
  //  discuss at: http://phpjs.org/functions/str_split/
  // original by: Martijn Wieringa
  // improved by: Brett Zamir (http://brett-zamir.me)
  // bugfixed by: Onno Marsman
  //  revised by: Theriault
  //  revised by: Rafał Kukawski (http://blog.kukawski.pl/)
  //    input by: Bjorn Roesbeke (http://www.bjornroesbeke.be/)
  //   example 1: str_split('Hello Friend', 3);
  //   returns 1: ['Hel', 'lo ', 'Fri', 'end']

  if (split_length == null) {
    split_length = 1;
  }
  if (string == null || split_length < 1) {
    return false;
  }
  string += '';
  var chunks = [],
    pos = 0,
    len = string.length;
  while (pos < len) {
    chunks.push(string.slice(pos, pos += split_length));
  }

  return chunks;
}


function count(string){
    var alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"];

    var splitted_string = str_split(string);

    var count = 0;
    for (i = 0; i < splitted_string.length; i++) { 
        var letterPosition = alphabet.indexOf(splitted_string[i])+1;
        count = count + letterPosition;
    }
    return count;
}

console.log(count("az")); // returns 27 in the console

答案 2 :(得分:3)

在JavaScript中,字符不是单字节数据类型,因此如果您想模仿C的工作方式,则需要自己创建映射。

例如,使用简单对象作为地图:

var characters: {
    'a': 1,
    'b': 2,
    ...
}

这样var number = charachters['a'];会将号码设置为1。其他人提供了简短的方法,这些方法很可能更为可行,这一方法主要是为了便于理解。

答案 3 :(得分:1)

你可以这样做

function convertToNumbers(str){
   var arr = "abcdefghijklmnopqrstuvwxyz".split("");
   return str.replace(/[a-z]/ig, function(m){ return arr.indexOf(m.toLowerCase()) + 1 });
}

您正在做的是创建一个字母数组,然后在String.replace函数中使用回调并返回字母+1的相应索引,因为索引从0开始

答案 4 :(得分:1)

这将有效

"abcdefghijklmnopqrstuvwxyz".split("").forEach(function (a,b,c){ console.log(a.toLowerCase().charCodeAt(0)-96)});


"iloveyou".split("").forEach(function (a,b,c){ console.log(a.toLowerCase().charCodeAt(0)-96)});

9
12
15
22
5
25
15
21

答案 5 :(得分:1)

您可以创建一个映射值的对象 -

function letterValue(str){
    var anum={
        a: 1, b: 2, c: 3, d: 4, e: 5, f: 6, g: 7, h: 8, i: 9, j: 10, k: 11, 
        l: 12, m: 13, n: 14,o: 15, p: 16, q: 17, r: 18, s: 19, t: 20, 
        u: 21, v: 22, w: 23, x: 24, y: 25, z: 26
    }
    if(str.length== 1) return anum[str] || ' ';
    return str.split('').map(letterValue);
}

letterValue('zoo')返回:(数组)[26,15,15];

letterValue('z')返回:(数字)26

答案 6 :(得分:0)

您只能得到ascii值,大写字母为负64。

var letterPlacement = "A".charCodeAt(0) - 64;

对于小写字母为负96。

var letterPlacement = "a".charCodeAt(0) - 96;

或者作为一个很好而整洁的单行函数,它不会给案件带来麻烦:

function alphabetifier(letter) {
    return letter.charCodeAt(0) - (letter === letter.toLowerCase() ? 96 : 64);
}      

答案 7 :(得分:0)

function convertAlpha(ch){
  //ASCII code of a is 97 so subtracting 96 would give 1 and so on
  return ch.charCodeAt(0)-96;
}

console.log(convertAlpha("b")); // returns 2

可以使用if子句对大写字母执行类似的操作。对于大写字母,我们会减去64,因为ASCII值“ A”为65,所以65-64 = 1,对于其他字母,依此类推。