Czy ktoś wie o bibliotece JavaScript (np. Podkreślenie, jQuery, MooTools itp.), Która oferuje metodę zwiększania litery?Jaką metodę można zastosować do zwiększania liczby liter?
Chciałbym móc zrobić coś takiego:
"a"++; // would return "b"
Czy ktoś wie o bibliotece JavaScript (np. Podkreślenie, jQuery, MooTools itp.), Która oferuje metodę zwiększania litery?Jaką metodę można zastosować do zwiększania liczby liter?
Chciałbym móc zrobić coś takiego:
"a"++; // would return "b"
function nextChar(c) {
return String.fromCharCode(c.charCodeAt(0) + 1);
}
nextChar('a');
Proste rozwiązanie, ale nie radzi sobie z wystąpieniem "z" lub "Z". – Trent
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
Hmm. Potrzebuje więcej jQuery. – Jasper
Plain JavaScript powinno załatwić sprawę:
String.fromCharCode('A'.charCodeAt() + 1) // Returns B
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/
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));
}
Jak z niego korzystać:
charLoop("A", "K", function(char) {
//char is one letter of the sequence
});
do pracy demo. – KNU
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>
Błąd "nie zdefiniowano". – SeanKendle
Zmieniono 'if (same (u, 'Z')) {' to 'if (u == 'Z') {' i działa idealnie, dziękuję! – SeanKendle
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. –
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'
Nie działa dla dużych liter –
Teraz działa również dla dużych liter :) – NikK
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);
}
};
Nie jestem pewien, czy * składnia *, której szukasz, jest możliwa, ale operacja jest możliwa za pomocą metod. – anson
Co to jest aplikacja? – valentinas