43

I am build an autocomplete that searches off of a CouchDB View.

I need to be able to take the final character of the input string, and replace the last character with the next letter of the english alphabet. (No need for i18n here)

For Example:

  • Input String = "b"
  • startkey = "b"
  • endkey = "c"

OR

  • Input String = "foo"
  • startkey = "foo"
  • endkey = "fop"

(in case you're wondering, I'm making sure to include the option inclusive_end=false so that this extra character doesn't taint my resultset)


The Question

  • Is there a function natively in Javascript that can just get the next letter of the alphabet?
  • Or will I just need to suck it up and do my own fancy function with a base string like "abc...xyz" and indexOf()?

7 Answers 7

67
my_string.substring(0, my_string.length - 1)
      + String.fromCharCode(my_string.charCodeAt(my_string.length - 1) + 1)
2
  • 10
    but watch for the edge case of z/Z z +1 = { and Z +1 = [ Feb 13, 2010 at 5:42
  • @ictoofay Thanks, this worked right away. @Gaby Thanks for the heads up! Feb 13, 2010 at 5:55
33

// This will return A for Z and a for z.

function nextLetter(s){
    return s.replace(/([a-zA-Z])[^a-zA-Z]*$/, function(a){
        var c= a.charCodeAt(0);
        switch(c){
            case 90: return 'A';
            case 122: return 'a';
            default: return String.fromCharCode(++c);
        }
    });
}
1
  • To be honest I was expecting something alongside the speed of 'a'++, not a whole regex Aug 13, 2019 at 10:54
19

A more comprehensive solution, which gets the next letter according to how MS Excel numbers it's columns... A B C ... Y Z AA AB ... AZ BA ... ZZ AAA

This works with small letters, but you can easily extend it for caps too.

getNextKey = function(key) {
  if (key === 'Z' || key === 'z') {
    return String.fromCharCode(key.charCodeAt() - 25) + String.fromCharCode(key.charCodeAt() - 25); // AA or aa
  } else {
    var lastChar = key.slice(-1);
    var sub = key.slice(0, -1);
    if (lastChar === 'Z' || lastChar === 'z') {
      // If a string of length > 1 ends in Z/z,
      // increment the string (excluding the last Z/z) recursively,
      // and append A/a (depending on casing) to it
      return getNextKey(sub) + String.fromCharCode(lastChar.charCodeAt() - 25);
    } else {
      // (take till last char) append with (increment last char)
      return sub + String.fromCharCode(lastChar.charCodeAt() + 1);
    }
  }
  return key;
};
4
  • 1
    Note, you can alternatively use key.substr(-1) in the else{} part to get the last character, but it doesn't work in IE's JScript, which doesn't accept negative indices for substr(). I'm not sure about the support in the new Edge browser though...
    – kumarharsh
    Jul 21, 2015 at 13:44
  • I've found that IE11 and Edge both do support negative indices in substr()... Seems like IE10 also does (tested by changing the browser mode in IE11)
    – kumarharsh
    Sep 18, 2015 at 12:37
  • This doesn't return keys in the order that you promised. You promised A B C ... Y Z AA AB ... AZ BA ... ZZ AAA But it actually returns A B .. Y Z ZA ZB .. ZY ZZ ZZA ZZB .. ZZY ZZZ ZZZA ZZZB ...
    – Wyck
    Sep 23, 2016 at 3:38
  • @Wyck - thanks for pointing this out, The script was wrong. I've fixed the script. Although I think your comment was a bit wrong: the function was outputting AA AB ... AZ A[ but it was outputting ZZA....ZZZ AAAA correctly (atleast from my testing)
    – kumarharsh
    Sep 23, 2016 at 8:41
7

Here is a function that does the same thing (except for upper case only, but that's easy to change) but uses slice only once and is iterative rather than recursive. In a quick benchmark, it's about 4 times faster (which is only relevant if you make really heavy use of it!).

function nextString(str) {
  if (! str)
    return 'A'  // return 'A' if str is empty or null

  let tail = ''
  let i = str.length -1
  let char = str[i]
  // find the index of the first character from the right that is not a 'Z'
  while (char === 'Z' && i > 0) {
    i--
    char = str[i]
    tail = 'A' + tail   // tail contains a string of 'A'
  }
  if (char === 'Z') // the string was made only of 'Z'
    return 'AA' + tail
  // increment the character that was not a 'Z'
  return str.slice(0, i) + String.fromCharCode(char.charCodeAt(0) + 1) + tail
}
4

Just to explain the main part of the code that Bipul Yadav wrote (can't comment yet due to lack of reps). Without considering the loop, and just taking the char "a" as an example:

"a".charCodeAt(0) = 97...hence "a".charCodeAt(0) + 1 = 98 and String.fromCharCode(98) = "b"...so the following function for any letter will return the next letter in the alphabet:

function nextLetterInAlphabet(letter) {
  if (letter == "z") {
    return "a";
  } else if (letter == "Z") {
    return "A";
  } else {
    return String.fromCharCode(letter.charCodeAt(0) + 1);
  }
}
1
var input = "Hello";
var result = ""
for(var i=0;i<input.length;i++)
{
  var curr = String.fromCharCode(input.charCodeAt(i)+1);
  result = result +curr;
}
console.log(result);
1
  • 1
    Please add some explanation as to why this answer is useful and how it works. Code-only answers are of low value, especially in the long term. Assume readers don't know as much as you do and so won't be able to understand your code.
    – TylerH
    Oct 28, 2019 at 19:53
0

I understand the original question was about moving the last letter of the string forward to the next letter. But I came to this question more interested personally in changing all the letters in the string, then being able to undo that. So I took the code written by Bipul Yadav and I added some more code. The below code takes a series of letters, increments each of them to the next letter maintaining case (and enables Zz to become Aa), then rolls them back to the previous letter (and allows Aa to go back to Zz).

var inputValue = "AaZzHello";
console.log( "starting value=[" + inputValue + "]" );

var resultFromIncrementing = ""
for( var i = 0; i < inputValue.length; i++ ) {
    var curr = String.fromCharCode( inputValue.charCodeAt(i) + 1 );
    if( curr == "[" ) curr = "A";
    if( curr == "{" ) curr = "a";
    resultFromIncrementing = resultFromIncrementing + curr;
}
console.log( "resultFromIncrementing=[" + resultFromIncrementing + "]" );

inputValue = resultFromIncrementing;
var resultFromDecrementing = "";
for( var i2 = 0; i2 < inputValue.length; i2++ ) {
    var curr2 = String.fromCharCode( inputValue.charCodeAt(i2) - 1 );
    if( curr2 == "@" ) curr2 = "Z";
    if( curr2 == "`" ) curr2 = "z";
    resultFromDecrementing = resultFromDecrementing + curr2;
}
console.log( "resultFromDecrementing=[" + resultFromDecrementing + "]" );

The output of this is:

starting value=[AaZzHello]
resultFromIncrementing=[BbAaIfmmp]
resultFromDecrementing=[AaZzHello]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.