Increment a string field that ends with a number by 1
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-07-2023 07:14 PM
Hi,
We have a form automation that creates a new server. One of the requirements is to check to see if the server requested has already been created in our CMDB and if it already exist then check for the next available number.
Our servers have a naming convention like the following:
CYSQLSNT01
If a server already exist like the one above, then they want me to increment the last numbers of the name. So the next would be CYSQLSNT02 or CYSQLSNT03.
Based on the requirements, the last 2 characters of the server name will always be a 2 digit number.
How can I increment this number? ParseInt didn't work. It came us as NaN.
I also need to make sure that the numbers start with 0, if it is one number. For instance, if the number is 5 then it would be 05 or if the number is 16, then it would just be 16. The last 2 characters are 2 digit numbers.
Any help would be appreciated.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-08-2023 01:09 AM
Hi @Dazler ,
Can you please share the code snippet which you have tried?
Thanks and Regards,
Rahul
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-08-2023 02:20 AM
function zeroPad(numberStr) {
return numberStr.padStart(2, "0");
}
var numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
numbers.forEach(
function(num) {
var numString = num.toString();
var paddedNum = zeroPad(numString);
console.log(paddedNum);
}
You try this to add zero if the number is coimg under (0 - 9). And then append this into the name string value

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-08-2023 02:52 AM
Hi,
What should happen if the number 99 is reached?
What would the next server be named then?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-08-2023 06:28 AM