什么是可用于增加字母的方法?

and*_*ser 90 javascript increment alphabetical

有没有人知道一个Javascript库(例如下划线,jQuery,MooTools等),它提供了一种递增字母的方法?

我希望能够做到这样的事情:

"a"++; // would return "b"
Run Code Online (Sandbox Code Playgroud)

Nat*_*all 159

function nextChar(c) {
    return String.fromCharCode(c.charCodeAt(0) + 1);
}
nextChar('a');
Run Code Online (Sandbox Code Playgroud)

  • 一种嗡嗡声,它会变成特殊字符,例如/ (3认同)
  • 简单的解决方案,但不处理“z”或“Z”的出现。 (2认同)
  • Daniel Thompson 这个解决方案提供了足够多的信息,您可以自己处理极端情况。毕竟,这是一个“互相帮助”的网站,而不是免费做我的工作的网站。 (2认同)

Zar*_*Zar 44

普通的JavaScript应该可以做到这一点:

String.fromCharCode('A'.charCodeAt() + 1) // Returns B
Run Code Online (Sandbox Code Playgroud)


Ron*_*ton 19

如果给定的字母是z怎么办?这是一个更好的解决方案.它包括A,B,C ...... X,Y,Z,AA,AB,......等基本上它会增加字母,就像Excel电子表格的列ID一样.

nextChar( 'YZ'); //返回"ZA"

    function nextChar(c) {
        var u = c.toUpperCase();
        if (same(u,'Z')){
            var txt = '';
            var i = u.length;
            while (i--) {
                txt += 'A';
            }
            return (txt+'A');
        } else {
            var p = "";
            var q = "";
            if(u.length > 1){
                p = u.substring(0, u.length - 1);
                q = String.fromCharCode(p.slice(-1).charCodeAt(0));
            }
            var l = u.slice(-1).charCodeAt(0);
            var z = nextLetter(l);
            if(z==='A'){
                return p.slice(0,-1) + nextLetter(q.slice(-1).charCodeAt(0)) + z;
            } else {
                return p + z;
            }
        }
    }
    
    function nextLetter(l){
        if(l<90){
            return String.fromCharCode(l + 1);
        }
        else{
            return 'A';
        }
    }
    
    function same(str,char){
        var i = str.length;
        while (i--) {
            if (str[i]!==char){
                return false;
            }
        }
        return true;
    }

// below is simply for the html sample interface and is unrelated to the javascript solution

var btn = document.getElementById('btn');
var entry = document.getElementById('entry');
var node = document.createElement("div");
node.id = "node";

btn.addEventListener("click", function(){
  node.innerHTML = '';
  var textnode = document.createTextNode(nextChar(entry.value));
  node.appendChild(textnode);
  document.body.appendChild(node);
});
Run Code Online (Sandbox Code Playgroud)
<input id="entry" type="text"></input>
<button id="btn">enter</button>
Run Code Online (Sandbox Code Playgroud)


小智 7

一种可能的方式如下定义

function incrementString(value) {
  let carry = 1;
  let res = '';

  for (let i = value.length - 1; i >= 0; i--) {
    let char = value.toUpperCase().charCodeAt(i);

    char += carry;

    if (char > 90) {
      char = 65;
      carry = 1;
    } else {
      carry = 0;
    }

    res = String.fromCharCode(char) + res;

    if (!carry) {
      res = value.substring(0, i) + res;
      break;
    }
  }

  if (carry) {
    res = 'A' + res;
  }

  return res;
}

console.info(incrementString('AAA')); // will print AAB
console.info(incrementString('AZA')); // will print AZB
console.info(incrementString('AZ')); // will print BA
console.info(incrementString('AZZ')); // will print BAA
console.info(incrementString('ABZZ')); // will print ACAA
console.info(incrementString('BA')); // will print BB
console.info(incrementString('BAB')); // will print BAC

// ... and so on ...

Run Code Online (Sandbox Code Playgroud)