How to get the next letter of the alphabet in Javascript?

Dominic Barnes picture Dominic Barnes · Feb 13, 2010 · Viewed 37.3k times · Source

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()?

Answer

icktoofay picture icktoofay · Feb 13, 2010
my_string.substring(0, my_string.length - 1)
      + String.fromCharCode(my_string.charCodeAt(my_string.length - 1) + 1)