Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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()?
like image 642
Dominic Barnes Avatar asked Feb 13 '10 05:02

Dominic Barnes


People also ask

Can you increment a char in JavaScript?

Use the String. fromCharCode() method to increment a letter in JavaScript, e.g. String. fromCharCode(char. charCodeAt(0) + 1) .

What is the use of \n in JavaScript?

The \n character matches newline characters.

Is alphabet function in JavaScript?

To check if a character is a letter, call the test() method on the following regular expression - /^[a-zA-Z]+$/ . If the character is a letter, the test method will return true , otherwise false will be returned. Copied!

How do you find first occurrence of a character in a string JavaScript?

JavaScript String indexOf() The indexOf() method returns the position of the first occurrence of a value in a string. The indexOf() method returns -1 if the value is not found. The indexOf() method is case sensitive.


1 Answers

my_string.substring(0, my_string.length - 1)       + String.fromCharCode(my_string.charCodeAt(my_string.length - 1) + 1) 
like image 51
icktoofay Avatar answered Sep 23 '22 18:09

icktoofay