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

JavascriptIncrementAlphabetical

Javascript Problem Overview


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 something like:

"a"++; // would return "b"

Javascript Solutions


Solution 1 - Javascript

Simple, direct solution

function nextChar(c) {
    return String.fromCharCode(c.charCodeAt(0) + 1);
}
nextChar('a');

As others have noted, the drawback is it may not handle cases like the letter 'z' as expected. But it depends on what you want out of it. The solution above will return '{' for the character after 'z', and this is the character after 'z' in ASCII, so it could be the result you're looking for depending on what your use case is.


Unique string generator

(Updated 2019/05/09)

Since this answer has received so much visibility I've decided to expand it a bit beyond the scope of the original question to potentially help people who are stumbling on this from Google.

I find that what I often want is something that will generate sequential, unique strings in a certain character set (such as only using letters), so I've updated this answer to include a class that will do that here:

class StringIdGenerator {
  constructor(chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ') {
    this._chars = chars;
    this._nextId = [0];
  }

  next() {
    const r = [];
    for (const char of this._nextId) {
      r.unshift(this._chars[char]);
    }
    this._increment();
    return r.join('');
  }

  _increment() {
    for (let i = 0; i < this._nextId.length; i++) {
      const val = ++this._nextId[i];
      if (val >= this._chars.length) {
        this._nextId[i] = 0;
      } else {
        return;
      }
    }
    this._nextId.push(0);
  }

  *[Symbol.iterator]() {
    while (true) {
      yield this.next();
    }
  }
}

Usage:

const ids = new StringIdGenerator();

ids.next(); // 'a'
ids.next(); // 'b'
ids.next(); // 'c'

// ...
ids.next(); // 'z'
ids.next(); // 'A'
ids.next(); // 'B'

// ...
ids.next(); // 'Z'
ids.next(); // 'aa'
ids.next(); // 'ab'
ids.next(); // 'ac'

Solution 2 - Javascript

Plain javascript should do the trick:

String.fromCharCode('A'.charCodeAt() + 1) // Returns B

Solution 3 - Javascript

What if the given letter is z? Here is a better solution. It goes A,B,C... X,Y,Z,AA,AB,... etc. Basically it increments letters like the column ID's of an Excel spreadsheet.

nextChar('yz'); // returns "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);
});

<input id="entry" type="text"></input>
<button id="btn">enter</button>

Solution 4 - Javascript

One possible way could be as defined below

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 ...

Solution 5 - Javascript

You can try this

console.log( 'a'.charCodeAt​(0))​

First convert it to Ascii number .. Increment it .. then convert from Ascii to char..

var nex = 'a'.charCodeAt(0);
console.log(nex)
$('#btn1').on('click', function() {
   var curr = String.fromCharCode(nex++)
   console.log(curr)
});

​Check FIDDLE

Solution 6 - Javascript

I needed to use sequences of letters multiple times and so I made this function based on this SO question. I hope this can help others.

function charLoop(from, to, callback)
{
    var i = from.charCodeAt(0);
    var to = to.charCodeAt(0);
    for(;i<=to;i++) callback(String.fromCharCode(i));
}
  • from - start letter
  • to - last letter
  • callback(letter) - function to execute for each letter in the sequence

How to use it:

charLoop("A", "K", function(char) {
    //char is one letter of the sequence
});

See this working demo

Solution 7 - Javascript

Adding upon all these answers:

// first code on page
String.prototype.nextChar = function(i) {
    var n = i | 1;
    return String.fromCharCode(this.charCodeAt(0) + n);
}
    
String.prototype.prevChar = function(i) {
    var n = i | 1;
    return String.fromCharCode(this.charCodeAt(0) - n);
}

Example: http://jsfiddle.net/pitaj/3F5Qt/

Solution 8 - Javascript

This one does work well:

var nextLetter = letter => {
    let charCode = letter.charCodeAt(0);
    let isCapital = letter == letter.toUpperCase();
    
    if (isCapital == true) {
        return String.fromCharCode((charCode - 64) % 26 + 65)
    } else {
        return String.fromCharCode((charCode - 96) % 26 + 97)
    }
}

EXAMPLES

nextLetter("a"); // returns 'b'
nextLetter("z"); // returns 'a'
nextLetter("A"); // returns 'B'
nextLetter("Z"); // returns 'A'

Solution 9 - Javascript

A just for laughs solution

function nextLetter(str) {
  const Alphabet = [
    // lower case 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",
    // upper case 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"
  ];

  const LetterArray = str.split("").map(letter => {
    if (Alphabet.includes(letter) === true) {
      return Alphabet[Alphabet.indexOf(letter) + 1];
    } else {
      return " ";
    }
  });

  const Assemble = () => LetterArray.join("").trim();
  return Assemble();
}


console.log(nextLetter("hello*3"));

Solution 10 - Javascript

Here is a variation of the rot13 algorithm I submitted on https://stackoverflow.com/a/28490254/881441:

function rot1(s) {
  return s.replace(/[A-Z]/gi, c =>
    "BCDEFGHIJKLMNOPQRSTUVWXYZAbcdefghijklmnopqrstuvwxyza"[
    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".indexOf(c) ] )
}

The input code in the bottom and the looked up codec is on the top (i.e. the output code is the same as the input code but shifted by 1). The function only changes letters, i.e. if any other character is passed in, it will be unchanged by this codec.

Solution 11 - Javascript

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);
}
};

Solution 12 - Javascript

Make a function with {a: 'b', b: 'c', etc} in a closure:-

let nextChar = (s => (
    "abcdefghijklmopqrstuvwxyza".split('')
    .reduce((a,b)=> (s[a]=b, b)), // make the lookup
c=> s[c] // the function returned
))({}); // parameter s, starts empty

usage:-

nextChar('a')

Adding uppercase and digits:-

let nextCh = (
    (alphabeta, s) => (
        [alphabeta, alphabeta.toUpperCase(), "01234567890"]
            .forEach(chars => chars.split('')
               .reduce((a,b) => (s[a]=b, b))), 
        c=> s[c] 
    )
)("abcdefghijklmopqrstuvwxyza", {});

p.s. In some versions of Javascript, you can use [...chars] instead of chars.split('')

Solution 13 - Javascript


function charLoop(from, to, callback) {
    var i = from.charCodeAt(0);
    var to = to.charCodeAt(0);
    for (; i <= to; i++) {
        callback(String.fromCharCode(i));
    }
}

var sequence = "";
charLoop("A", "Z", function (char) {
    sequence += char + " ";
});

sequence = sequence.trim();
sequence = sequence.split(" ");

var resseq = sequence;
var res = "";
var prevlet = "";
var nextlet = "";

for (b = 0; b < resseq.length; b++) {
    if (prevlet != "") {
        prevlet = resseq[b];
    }

    for (a = 0; a < sequence.length; a++) {
        for (j = 1; j < 100; j++) {
            if (prevlet == "") {
                prevlet = sequence[a];
                nextlet = sequence[a + 1];
                res += sequence[a] + sequence[a] + 0 + j + " ";
            }
            else {

                if (j < 10) {
                    res += prevlet + sequence[a] + 0 + j + " ";
                }
                else {
                    res += prevlet + sequence[a] + j + " ";
                }
            }
        }
    }
}

document.body.innerHTML = res;

Solution 14 - Javascript

Based on @Nathan wall answer increment and decrement

// Albhabet auto increment and decrement
class StringIdGenerator {
    constructor(chars = '') {
      this._chars = chars;
    }

  next() {
    var u = this._chars.toUpperCase();
    if (this._same(u,'Z')){
        var txt = '';
        var i = u.length;
        while (i--) {
            txt += 'A';
        }
        this._chars = 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 = this._nextLetter(l);
      if(z==='A'){
        this._chars = p.slice(0,-1) + this._nextLetter(q.slice(-1).charCodeAt(0)) + z;
          return p.slice(0,-1) + this._nextLetter(q.slice(-1).charCodeAt(0)) + z;
      } else {
        this._chars = p+z;
          return p + z;
      }
    }
  }

  prev() {
    var u = this._chars.toUpperCase();
    console.log("u "+u)
    var l = u.slice(-1).charCodeAt(0);
    var z = this._nextLetter(l);
    var rl = u.slice(1)
    var y = (rl == "A") ? "Z" :this._prevLetter(rl.charCodeAt(0))
      var txt = '';
      var i = u.length;
      var j = this._chars
      var change = false
      while (i--) {
        if(change){
          if (u[u.length-1] == "A"){
            txt += this._prevLetter(u[i].charCodeAt(0))
          }else{
            txt += u[i]
          }
          
        }else{
          if (u[u.length-1] == "A"){
            txt += this._prevLetter(u[i].charCodeAt(0))
            change = true
          }else{
            change = true
            txt += this._prevLetter(u[i].charCodeAt(0))
          }
        }
      }
      if(u == "A" && txt == "Z"){
        this._chars = ''
      }else{
        this._chars = this._reverseString(txt);
      }
      console.log(this._chars)
      return (j);
  }
  _reverseString(str) {
      return str.split("").reverse().join("");
  }
  _nextLetter(l){
      if(l<90){
          return String.fromCharCode(l + 1);
      }
      else{
          return 'A';
      }
  }

  _prevLetter(l){
    if(l<=90){
      if(l == 65) l = 91
        return String.fromCharCode(l-1);
    }
    else{
        return 'A';
    }
  }
  _same(str,char){
      var i = str.length;
      while (i--) {
          if (str[i]!==char){
              return false;
          }
      }
      return true;
  }
    
}

Usage

const ids = new StringIdGenerator();

ids.next(); 
ids.prev();

Solution 15 - Javascript

This is my function to get increment letters to infinity in Javascript( for uppercase only)

function getNextStringId(str) {
    let index = str.length-1;
    let baseCode= str.charCodeAt(index);
    do{
        baseCode= str.charCodeAt(index);
        let strArr= str.split("");
        if(strArr[index] == "Z"){
            strArr[index] = "A";
            if(index==0){
                strArr.unshift("A");
            }
        }
        else{
            strArr[index]= String.fromCharCode(baseCode + 1);
        }
        str= strArr.join("");
        index--;
    } while(baseCode == 90)
    return str;
}


getNextStringId("A") // B
getNextStringId("Z") // AA
getNextStringId("ABZZ") // ACAA

Solution 16 - Javascript

Zar and Nathan's answers both good stuff. Everything else here seems overcomplicated for me.

  • I wanted to specify my own character set so didn't bother with charCode
  • I didn't need multi character recursion
  • added 0 to my character set for error handling (while (letter != 0))

I ended up with this:

let letter = "a";
let alphabet = "abcdefghijklmnopqrstuvwxyz0";
let nextletter = alphabet.substr(alphabet.indexOf(letter)+1,1);

Then I can very simply add in further dimensions:

let alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!£$%^&*_0";

function increment(e) {
let letter = e.value;
let alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0";
let next = alphabet.substr(alphabet.indexOf(letter)+1,1);
document.getElementById("result").innerHTML = next;
};

<input type="text" minlength="1" maxlength="1" onchange="increment(this)" placeholder="type letter">
<button>Update</button>
<div id="result"></div>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionandyzinsserView Question on Stackoverflow
Solution 1 - JavascriptNathan WallView Answer on Stackoverflow
Solution 2 - JavascriptZarView Answer on Stackoverflow
Solution 3 - JavascriptRonnie RoystonView Answer on Stackoverflow
Solution 4 - JavascriptSandeep SinghView Answer on Stackoverflow
Solution 5 - JavascriptSushanth --View Answer on Stackoverflow
Solution 6 - JavascriptletiagoalvesView Answer on Stackoverflow
Solution 7 - JavascriptPitaJView Answer on Stackoverflow
Solution 8 - JavascriptNikKView Answer on Stackoverflow
Solution 9 - Javascriptdean schmidView Answer on Stackoverflow
Solution 10 - JavascriptStephen QuanView Answer on Stackoverflow
Solution 11 - JavascriptmrtyormaaView Answer on Stackoverflow
Solution 12 - JavascriptQuentin 2View Answer on Stackoverflow
Solution 13 - JavascriptLokeshKumarView Answer on Stackoverflow
Solution 14 - JavascriptGowtham SooryarajView Answer on Stackoverflow
Solution 15 - JavascriptManoj RanaView Answer on Stackoverflow
Solution 16 - Javascript5DiraptorView Answer on Stackoverflow