[英]Increment digit part of string in JavaScript
我有一個結尾處包含數字的字符串。 當某些動作發生時,我想將數字部分增加1
。
例如
var myString = 'AA11111'
increaseStringValue(myString)
# myString new value => 'AA11112'
另外,當字符串值達到“ AA99999”時如何增加字符,因此字符串的新值將為“ AB11111”?
您可以拆分char和digit部分,以便分別處理它們。
喜歡:
function increaseStringValue(str){
let charPart = str.substring(0,2);
let digitPart = str.substring(2);
digitPart = +digitPart+1
if(digitPart >= 99999){
digitPart = 11111;
if(charPart[1] == 'Z'){
if(charPart[0] == 'Z'){
throw 'Overflow happened'
}
charPart = String.fromCharCode(charPart.charCodeAt(0)+1) + 'A'
}else{
charPart = charPart[0] + String.fromCharCode(charPart.charCodeAt(1)+1)
}
}
return charPart + digitPart;
}
increaseStringValue('AA11111'); // 'AA11112'
increaseStringValue('AA99999'); // 'AB11111'
increaseStringValue('AZ99999'); // 'BA11111'
increaseStringValue('ZZ99999'); // Exception: Overflow happened
該鏈接將對您有所幫助:
編輯:
以下函數將適合具有char和digit動態位置的未知長度的字符串。
function increaseStringValue(str) {
let charOverFlowed = true;
let result = ""
for (let i = str.length - 1; i >= 0; i--) {
let currentChar = str[i];
if ('123456789'.indexOf(currentChar) !== -1) {
if (charOverFlowed) {
currentChar = +currentChar + 1
charOverFlowed = false;
}
if (currentChar > 9) {
currentChar = 1;
charOverFlowed = true;
}
} else if (charOverFlowed) {
currentChar = String.fromCharCode(currentChar.charCodeAt(0) + 1)
charOverFlowed = false;
if (currentChar > 'Z') {
if(i == 0){
throw 'Overflow Happened'
}
currentChar = 'A'
charOverFlowed = true
}
}
result = currentChar + result;
}
return result;
}
increaseStringValue('AAAACA')
// "AAAACB"
increaseStringValue('AAAACA1111')
// "AAAACA1112"
increaseStringValue('A1')
// "A2"
increaseStringValue('Z')
// Uncaught Overflow Happened
increaseStringValue('A1999')
// "A2111"
function increaseStringValue(myString){
return myString.replace(/\d+/ig, function(a){ return a*1+1;});
}
console.log(increaseStringValue("asg61"));
對於下一個問題:
function increaseStringValue(myString){
return myString.replace(/(A)(\d+)/ig, function(a, b, c){
var r = c*1+1; return r==99999+1?"B11111":"A"+r;
});
}
console.log(increaseStringValue("AA99999"));
和整個方式:
function increaseStringValue(myString){
return myString.replace(/([a-e])(\d+)/ig, function(a, b, c){
var r = c*1+1; return r==99999+1?String.fromCharCode(a.charCodeAt(0)+1)+"11111":b+r;
});
}
console.log(increaseStringValue("AB99999"));
請找到有用的代碼段。 如果這是您所期望的。
let stringNum = 'AA11111';//initialise string let clickTriggered = ()=>{ let startString = "AA"; let newNum = ()=>{ let numberPart = stringNum.split("AA")[1]; let lastChar = stringNum[stringNum.length-1]; return Number(numberPart) != NaN || Number(numberPart) <= 99999 ? Number(numberPart)+1 : 11111; }; stringNum = `${startString}${newNum()}` console.log(stringNum) }
<h1 onclick="clickTriggered()">click here</h1>
您可以使用String#replace
並在string#replace
的函數回調中提供增量邏輯。
const increaseStringValue = (str) => str.replace(/\\d+$/, n => n === '99999' ? 11111 : +n + 1); console.log(increaseStringValue('AA99999')); console.log(increaseStringValue('AA11315')); console.log(increaseStringValue('AA11111'));
我用這個解決方案解決了
let app = new Vue({ el: '#app', data: { text: "AA995" }, methods: { addOneString: function(str) { var alphabet = 'abcdefghijklmnopqrstuvwxyz', length = alphabet.length, result = str, i = str.length, value = str; while(i >= 0) { var last = str.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; } } console.log("result",result); if (value !== result ) this.text = result; } } })
<script src="https://unpkg.com/vue/dist/vue.js"></script> <div class="container" id="app"> <button @click="addOneString(text)">Add one</button> </br> <p> {{text}} </p> </div>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.