Increment a string field that ends with a number by 1

Dazler
Mega Sage

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.

10 REPLIES 10

Rahul Talreja
Mega Sage
Mega Sage

Hi @Dazler ,
Can you please share the code snippet which you have tried?

Please mark my response correct/helpful as applicable!
Thanks and Regards,
Rahul

Christopher096
Tera Guru
Tera Guru

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

OlaN
Giga Sage
Giga Sage

Hi,

What should happen if the number 99 is reached?

What would the next server be named then?

Hi @OlaN 

 

If it reaches 99, then the team wants a ticket to let them know that it has reach 99.