Does anyone know of a Javascript library (e.g. underscore, jQuery, MooTools, etc.) that offers a method of incrementing a letter?
有谁知道Javascript库(例如下划线、jQuery、MooTools等)提供递增字母的方法吗?
I would like to be able to do something like:
我想做的是:
"a"++; // would return "b"
8 个解决方案
#1
119
function nextChar(c) {
return String.fromCharCode(c.charCodeAt(0) + 1);
}
nextChar('a');
#2
37
Plain javascript should do the trick:
简单的javascript就可以做到:
String.fromCharCode('A'.charCodeAt() + 1) // Returns B
#3
16
What if the given letter is z? Here is a better solution. It goes A,B,C... X,Y,Z,AA,AB,... etc. Basically it increments letters like the column ID's of an Excel spreadsheet.
如果给定的字母是z呢?这里有一个更好的解决方案。它是A,B,C…X,Y,Z、AA、AB,……等等。基本上它会增加字母,比如Excel电子表格的列ID。
nextChar('yz'); // returns "ZA"
nextChar(yz);/ /返回“咱”
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>
#4
3
You can try this
你可以试试这个
console.log( 'a'.charCodeAt(0))
First convert it to Ascii number .. Increment it .. then convert from Ascii to char..
首先把它转换成Ascii码。将它加. .然后从Ascii码转换为char。
var nex = 'a'.charCodeAt(0);
console.log(nex)
$('#btn1').on('click', function() {
var curr = String.fromCharCode(nex++)
console.log(curr)
});
Check FIDDLE
检查小提琴
#5
3
I needed to use sequences of letters multiple times and so I made this function based on this SO question. I hope this can help others.
我需要多次使用字母序列所以我基于这个so问题做了这个函数。我希望这能帮助别人。
function charLoop(from, to, callback)
{
var i = from.charCodeAt(0);
var to = to.charCodeAt(0);
for(;i<=to;i++) callback(String.fromCharCode(i));
}
- from - start letter
- 从- - -开始信
- to - last letter
- ——最后一个字母
- callback(letter) - function to execute for each letter in the sequence
- 回调(字母)-函数执行序列中的每个字母
How to use it:
如何使用它:
charLoop("A", "K", function(char) {
//char is one letter of the sequence
});
看到这个演示工作
#6
2
Adding upon all these answers:
加上所有这些答案:
// 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);
}
Example: http://jsfiddle.net/pitaj/3F5Qt/
例如:http://jsfiddle.net/pitaj/3F5Qt/
#7
1
This one does work well:
这个方法确实很有效:
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'
#8
0
This is really old. But I needed this functionality and none of the solutions are optimal for my use case. I wanted to generate a, b, c...z, aa,ab...zz, aaa... . This simple recursion does the job.
这真的是老了。但是我需要这个功能,没有一个解决方案适合我的用例。我想生成a b c…z,aa、ab……zz,aaa…。这个简单的递归就可以了。
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);
}
};
#1
119
function nextChar(c) {
return String.fromCharCode(c.charCodeAt(0) + 1);
}
nextChar('a');
#2
37
Plain javascript should do the trick:
简单的javascript就可以做到:
String.fromCharCode('A'.charCodeAt() + 1) // Returns B
#3
16
What if the given letter is z? Here is a better solution. It goes A,B,C... X,Y,Z,AA,AB,... etc. Basically it increments letters like the column ID's of an Excel spreadsheet.
如果给定的字母是z呢?这里有一个更好的解决方案。它是A,B,C…X,Y,Z、AA、AB,……等等。基本上它会增加字母,比如Excel电子表格的列ID。
nextChar('yz'); // returns "ZA"
nextChar(yz);/ /返回“咱”
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>
#4
3
You can try this
你可以试试这个
console.log( 'a'.charCodeAt(0))
First convert it to Ascii number .. Increment it .. then convert from Ascii to char..
首先把它转换成Ascii码。将它加. .然后从Ascii码转换为char。
var nex = 'a'.charCodeAt(0);
console.log(nex)
$('#btn1').on('click', function() {
var curr = String.fromCharCode(nex++)
console.log(curr)
});
Check FIDDLE
检查小提琴
#5
3
I needed to use sequences of letters multiple times and so I made this function based on this SO question. I hope this can help others.
我需要多次使用字母序列所以我基于这个so问题做了这个函数。我希望这能帮助别人。
function charLoop(from, to, callback)
{
var i = from.charCodeAt(0);
var to = to.charCodeAt(0);
for(;i<=to;i++) callback(String.fromCharCode(i));
}
- from - start letter
- 从- - -开始信
- to - last letter
- ——最后一个字母
- callback(letter) - function to execute for each letter in the sequence
- 回调(字母)-函数执行序列中的每个字母
How to use it:
如何使用它:
charLoop("A", "K", function(char) {
//char is one letter of the sequence
});
看到这个演示工作
#6
2
Adding upon all these answers:
加上所有这些答案:
// 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);
}
Example: http://jsfiddle.net/pitaj/3F5Qt/
例如:http://jsfiddle.net/pitaj/3F5Qt/
#7
1
This one does work well:
这个方法确实很有效:
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'
#8
0
This is really old. But I needed this functionality and none of the solutions are optimal for my use case. I wanted to generate a, b, c...z, aa,ab...zz, aaa... . This simple recursion does the job.
这真的是老了。但是我需要这个功能,没有一个解决方案适合我的用例。我想生成a b c…z,aa、ab……zz,aaa…。这个简单的递归就可以了。
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);
}
};