"b" "aa".next # => "ab" "z".next # => "aa" # two a's after one-6ren">
gpt4 book ai didi

javascript - 如何在 Javascript(Node.js) 中进行字符串递增?

转载 作者:搜寻专家 更新时间:2023-10-31 23:38:59 25 4
gpt4 key购买 nike

在 ruby 中,

"a".next [or] "a".succ # => "b"
"aa".next # => "ab"
"z".next # => "aa" # two a's after one z

如何在 Javascript 中使用它?像这样的东西:

incr_str("aaa") ===> aab
incr_str("zzz") ===> aaaa

最佳答案

A google search for "Ruby string succ Javascript"返回 this gist from Devon Govett called "An implementation of Ruby's string.succ method in JavaScript"这似乎是你所追求的......

/*
* An implementation of Ruby's string.succ method.
* By Devon Govett
*
* Returns the successor to str. The successor is calculated by incrementing characters starting
* from the rightmost alphanumeric (or the rightmost character if there are no alphanumerics) in the
* string. Incrementing a digit always results in another digit, and incrementing a letter results in
* another letter of the same case.
*
* If the increment generates a carry, the character to the left of it is incremented. This
* process repeats until there is no carry, adding an additional character if necessary.
*
* succ("abcd") == "abce"
* succ("THX1138") == "THX1139"
* succ("<<koala>>") == "<<koalb>>"
* succ("1999zzz") == "2000aaa"
* succ("ZZZ9999") == "AAAA0000"
*/

function succ(input) {
var alphabet = 'abcdefghijklmnopqrstuvwxyz',
length = alphabet.length,
result = input,
i = input.length;

while(i >= 0) {
var last = input.charAt(--i),
next = '',
carry = false;

if (isNaN(last)) {
index = alphabet.indexOf(last.toLowerCase());

if (index === -1) {
next = last;
carry = true;
}
else {
var isUpperCase = last === last.toUpperCase();
next = alphabet.charAt((index + 1) % length);
if (isUpperCase) {
next = next.toUpperCase();
}

carry = index + 1 >= length;
if (carry && i === 0) {
var added = isUpperCase ? 'A' : 'a';
result = added + next + result.slice(1);
break;
}
}
}
else {
next = +last + 1;
if(next > 9) {
next = 0;
carry = true
}

if (carry && i === 0) {
result = '1' + next + result.slice(1);
break;
}
}

result = result.slice(0, i) + next + result.slice(i + 1);
if (!carry) {
break;
}
}
return result;
}

关于javascript - 如何在 Javascript(Node.js) 中进行字符串递增?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18050889/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com