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

后端 未结 7 983
醉酒成梦
醉酒成梦 2021-01-03 18:20

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 wit

相关标签:
7条回答
  • 2021-01-03 18:58

    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);
      }
    }
    
    0 讨论(0)
提交回复
热议问题