![](/img/trans.png)
[英]How to on selected letter from alphabet show text for that letter? Vanilla Javascript
[英]How to get the next letter of the alphabet in Javascript?
我正在構建一個從 CouchDB 視圖中搜索的自動完成功能。
我需要能夠獲取輸入字符串的最后一個字符,並將最后一個字符替換為英文字母表的下一個字母。 (這里不需要 i18n)
例如:
或者
(如果您想知道,我確保包含選項inclusive_end=false
以便這個額外的字符不會污染我的結果集)
indexOf()
這樣的基本字符串來做我自己的花哨的函數?my_string.substring(0, my_string.length - 1)
+ String.fromCharCode(my_string.charCodeAt(my_string.length - 1) + 1)
// 這將返回 A 代表 Z 和 a 代表 z。
function nextLetter(s){
return s.replace(/([a-zA-Z])[^a-zA-Z]*$/, function(a){
var c= a.charCodeAt(0);
switch(c){
case 90: return 'A';
case 122: return 'a';
default: return String.fromCharCode(++c);
}
});
}
一個更全面的解決方案,它根據 MS Excel 的列數來獲取下一個字母... ABC ... YZ AA AB ... AZ BA ... ZZ AAA
這適用於小寫字母,但您也可以輕松地將其擴展為大寫字母。
getNextKey = function(key) {
if (key === 'Z' || key === 'z') {
return String.fromCharCode(key.charCodeAt() - 25) + String.fromCharCode(key.charCodeAt() - 25); // AA or aa
} else {
var lastChar = key.slice(-1);
var sub = key.slice(0, -1);
if (lastChar === 'Z' || lastChar === 'z') {
// If a string of length > 1 ends in Z/z,
// increment the string (excluding the last Z/z) recursively,
// and append A/a (depending on casing) to it
return getNextKey(sub) + String.fromCharCode(lastChar.charCodeAt() - 25);
} else {
// (take till last char) append with (increment last char)
return sub + String.fromCharCode(lastChar.charCodeAt() + 1);
}
}
return key;
};
這是一個做同樣事情的函數(除了大寫,但很容易改變)但只使用slice
一次並且是迭代而不是遞歸的。 在快速基准測試中,它大約快 4 倍(這僅在您大量使用它時才有意義!)。
function nextString(str) {
if (! str)
return 'A' // return 'A' if str is empty or null
let tail = ''
let i = str.length -1
let char = str[i]
// find the index of the first character from the right that is not a 'Z'
while (char === 'Z' && i > 0) {
i--
char = str[i]
tail = 'A' + tail // tail contains a string of 'A'
}
if (char === 'Z') // the string was made only of 'Z'
return 'AA' + tail
// increment the character that was not a 'Z'
return str.slice(0, i) + String.fromCharCode(char.charCodeAt(0) + 1) + tail
}
只是為了解釋 Bipul Yadav 編寫的代碼的主要部分(由於缺乏代表,還不能發表評論)。 不考慮循環,僅以字符“a”為例:
"a".charCodeAt(0) = 97 ...因此"a".charCodeAt(0) + 1 = 98和String.fromCharCode(98) = "b" ...因此對於任何字母的以下函數將返回字母表中的下一個字母:
function nextLetterInAlphabet(letter) {
if (letter == "z") {
return "a";
} else if (letter == "Z") {
return "A";
} else {
return String.fromCharCode(letter.charCodeAt(0) + 1);
}
}
var input = "Hello";
var result = ""
for(var i=0;i<input.length;i++)
{
var curr = String.fromCharCode(input.charCodeAt(i)+1);
result = result +curr;
}
console.log(result);
我知道最初的問題是將字符串的最后一個字母向前移動到下一個字母。 但是我個人對更改字符串中的所有字母更感興趣,然后能夠撤消該問題。 因此,我采用了 Bipul Yadav 編寫的代碼,並添加了更多代碼。 下面的代碼采用一系列字母,將每個字母遞增到下一個字母維護案例(並使 Zz 成為 Aa),然后將它們回滾到前一個字母(並允許 Aa 回到 Zz)。
var inputValue = "AaZzHello";
console.log( "starting value=[" + inputValue + "]" );
var resultFromIncrementing = ""
for( var i = 0; i < inputValue.length; i++ ) {
var curr = String.fromCharCode( inputValue.charCodeAt(i) + 1 );
if( curr == "[" ) curr = "A";
if( curr == "{" ) curr = "a";
resultFromIncrementing = resultFromIncrementing + curr;
}
console.log( "resultFromIncrementing=[" + resultFromIncrementing + "]" );
inputValue = resultFromIncrementing;
var resultFromDecrementing = "";
for( var i2 = 0; i2 < inputValue.length; i2++ ) {
var curr2 = String.fromCharCode( inputValue.charCodeAt(i2) - 1 );
if( curr2 == "@" ) curr2 = "Z";
if( curr2 == "`" ) curr2 = "z";
resultFromDecrementing = resultFromDecrementing + curr2;
}
console.log( "resultFromDecrementing=[" + resultFromDecrementing + "]" );
這個的輸出是:
starting value=[AaZzHello]
resultFromIncrementing=[BbAaIfmmp]
resultFromDecrementing=[AaZzHello]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.