2012-09-19 19 views

Odpowiedz

108
function nextChar(c) { 
    return String.fromCharCode(c.charCodeAt(0) + 1); 
} 
nextChar('a'); 
+0

Proste rozwiązanie, ale nie radzi sobie z wystąpieniem "z" lub "Z". – Trent

2

można spróbować to

console.log('a'.charCodeAt​(0))​ 

najpierw przekonwertować go na numer ASCII .. Przyrost go .. następnie przekonwertować z ASCII char ..

var nex = 'a'.charCodeAt(0); 
console.log(nex) 
$('#btn1').on('click', function() { 
    var curr = String.fromCharCode(nex++) 
    console.log(curr) 
}); 

Sprawdź FIDDLE

+1

Hmm. Potrzebuje więcej jQuery. – Jasper

34

Plain JavaScript powinno załatwić sprawę:

String.fromCharCode('A'.charCodeAt() + 1) // Returns B 
2

Dodanie po tych wszystkich odpowiedzi:

// first code on page 
String.prototype.nextChar = function(i) { 
    var n = i | 1; 
    return String.fromCharCode(this.charCodeAt(0) + n); 
} 

String.prototype.prevChar = function(i) { 
    var n = i | 1; 
    return String.fromCharCode(this.charCodeAt(0) - n); 
} 

Przykład: http://jsfiddle.net/pitaj/3F5Qt/

3

Musiałem użyć sekwencji liter wielokrotne więc zrobiłem tę funkcję w oparciu o to pytanie SO. Mam nadzieję, że to pomoże innym.

function charLoop(from, to, callback) 
{ 
    var i = from.charCodeAt(0); 
    var to = to.charCodeAt(0); 
    for(;i<=to;i++) callback(String.fromCharCode(i)); 
} 
  • od - startowym listu
  • do - ostatnim liście
  • zwrotnego (list) - funkcji do wykonania dla każdej litery w kolejności

Jak z niego korzystać:

charLoop("A", "K", function(char) { 
    //char is one letter of the sequence 
}); 

See this working demo

+1

do pracy demo. – KNU

12

Co jeśli dana litera jest z? Oto lepsze rozwiązanie. Przechodzi A, B, C ... X, Y, Z, AA, AB, itd. Zasadniczo zwiększa litery takie jak identyfikatory kolumn arkusza kalkulacyjnego Excel.

nextChar ("yz"); // zwraca "ZA"

function nextChar(c) { 
 
     var u = c.toUpperCase(); 
 
     if (same(u,'Z')){ 
 
      var txt = ''; 
 
      var i = u.length; 
 
      while (i--) { 
 
       txt += 'A'; 
 
      } 
 
      return (txt+'A'); 
 
     } else { 
 
      var p = ""; 
 
      var q = ""; 
 
      if(u.length > 1){ 
 
       p = u.substring(0, u.length - 1); 
 
       q = String.fromCharCode(p.slice(-1).charCodeAt(0)); 
 
      } 
 
      var l = u.slice(-1).charCodeAt(0); 
 
      var z = nextLetter(l); 
 
      if(z==='A'){ 
 
       return p.slice(0,-1) + nextLetter(q.slice(-1).charCodeAt(0)) + z; 
 
      } else { 
 
       return p + z; 
 
      } 
 
     } 
 
    } 
 
    
 
    function nextLetter(l){ 
 
     if(l<90){ 
 
      return String.fromCharCode(l + 1); 
 
     } 
 
     else{ 
 
      return 'A'; 
 
     } 
 
    } 
 
    
 
    function same(str,char){ 
 
     var i = str.length; 
 
     while (i--) { 
 
      if (str[i]!==char){ 
 
       return false; 
 
      } 
 
     } 
 
     return true; 
 
    } 
 

 
// below is simply for the html sample interface and is unrelated to the javascript solution 
 

 
var btn = document.getElementById('btn'); 
 
var entry = document.getElementById('entry'); 
 
var node = document.createElement("div"); 
 
node.id = "node"; 
 

 
btn.addEventListener("click", function(){ 
 
    node.innerHTML = ''; 
 
    var textnode = document.createTextNode(nextChar(entry.value)); 
 
    node.appendChild(textnode); 
 
    document.body.appendChild(node); 
 
});
<input id="entry" type="text"></input> 
 
<button id="btn">enter</button>

+0

Błąd "nie zdefiniowano". – SeanKendle

+0

Zmieniono 'if (same (u, 'Z')) {' to 'if (u == 'Z') {' i działa idealnie, dziękuję! – SeanKendle

+0

Cieszę się, że zadziałało i dziękuję za informację zwrotną. Być może ten początkowy błąd był bcs funkcja nie została wklejona tam "? Samo (str, char)"? Nie wiem. –

1

Ten działa dobrze:

var nextLetter = letter => { 
    let charCode = letter.charCodeAt(0); 
    let isCapital = letter == letter.toUpperCase(); 

    if (isCapital == true) { 
     return String.fromCharCode((charCode - 64) % 26 + 65) 
    } else { 
     return String.fromCharCode((charCode - 96) % 26 + 97) 
    } 
} 

EXAMPLES 

nextLetter("a"); // returns 'b' 
nextLetter("z"); // returns 'a' 
nextLetter("A"); // returns 'B' 
nextLetter("Z"); // returns 'A' 
+1

Nie działa dla dużych liter –

+0

Teraz działa również dla dużych liter :) – NikK

0

To jest bardzo stary. Ale potrzebowałem tej funkcjonalności i żadne z rozwiązań nie jest optymalne dla mojego przypadku użycia. Chciałem wygenerować a, b, c ... z, aa, ab ... zz, aaa .... Ta prosta rekursja wykonuje zadanie.

function nextChar(str) { 
if (str.length == 0) { 
    return 'a'; 
} 
var charA = str.split(''); 
if (charA[charA.length - 1] === 'z') { 
    return nextID(str.substring(0, charA.length - 1)) + 'a'; 
} else { 
    return str.substring(0, charA.length - 1) + 
     String.fromCharCode(charA[charA.length - 1].charCodeAt(0) + 1); 
} 
};