如何在Javascript中获取字母表的下一个字母? [英] How to get the next letter of the alphabet in Javascript?

查看:22
本文介绍了如何在Javascript中获取字母表的下一个字母?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个从 CouchDB 视图中搜索的自动完成功能.

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

我需要能够取输入字符串的最后一个字符,并将最后一个字符替换为英文字母表的下一个字母.(这里不需要 i18n)

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)

例如:

  • 输入字符串 = "b"
  • startkey = "b"
  • endkey = "c"
  • Input String = "b"
  • startkey = "b"
  • endkey = "c"

OR

  • 输入字符串 = "foo"
  • startkey = "foo"
  • endkey = "fop"
  • Input String = "foo"
  • startkey = "foo"
  • endkey = "fop"

(如果您想知道,我确保包含选项 inclusive_end=false 以便这个额外的字符不会污染我的结果集)

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

  • Javascript 中是否有一个函数可以获取字母表的下一个字母?
  • 或者我是否只需要把它搞定并使用诸如abc...xyz"和indexOf()之类的基本字符串来执行我自己的奇特函数?
  • 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()?

推荐答案

my_string.substring(0, my_string.length - 1)
      + String.fromCharCode(my_string.charCodeAt(my_string.length - 1) + 1)

这篇关于如何在Javascript中获取字母表的下一个字母?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆