What is a method that can be used to increment letters?

前端 未结 14 2812
我在风中等你
我在风中等你 2020-11-27 04:19

Does anyone know of a Javascript library (e.g. underscore, jQuery, MooTools, etc.) that offers a method of incrementing a letter?

I would like to be able to do somet

14条回答
  •  星月不相逢
    2020-11-27 04:32

    This is really old. But I needed this functionality and none of the solutions are optimal for my use case. I wanted to generate a, b, c...z, aa,ab...zz, aaa... . This simple recursion does the job.

    function nextChar(str) {
    if (str.length == 0) {
        return 'a';
    }
    var charA = str.split('');
    if (charA[charA.length - 1] === 'z') {
        return nextID(str.substring(0, charA.length - 1)) + 'a';
    } else {
        return str.substring(0, charA.length - 1) +
            String.fromCharCode(charA[charA.length - 1].charCodeAt(0) + 1);
    }
    };
    

提交回复
热议问题