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

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

问题描述

我正在构建一个自动补全功能,可以从CouchDB视图中进行搜索.

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

我需要能够接受输入字符串的最后一个字符,并用英语字母的下一个字母替换最后一个字符. (这里不需要i18​​n)

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"
  • 开始键 ="b"
  • 结束键 ="c"
  • Input String = "b"
  • startkey = "b"
  • endkey = "c"

OR

  • 输入字符串 ="foo"
  • 开始键 ="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)

  • Java语言中是否有一个本机函数可以获取字母表中的下一个字母?
  • 还是我只需要吸收它并使用诸如"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天全站免登陆