Rahul
Rahul

Reputation: 759

How to subtract and retain leading zeros from a string

I have to subtract -1 from the string PR001-CC001578 and pass it as parameter in the xpath to identify an element. Am splitting it with CC and subtracting -1 from 001578. The result is 1577. But the leading zeros are removed because of which the xpath identification is failing.

        let courseID = "PR001-CC001578";
        let currCourseID = courseID.split('CC');
        let otherCourseID = currCourseID[1]-1;
        console.info("other Course ID:", otherCourseID);
        var courseIDAssetsPg="//div[contains(text(),'%d')]";
        var replaceCCId = courseIDAssetsPg.replace("%d", otherCourseID);
        var CCIdLoc = element(by.xpath(replaceCCId));
        console.info("locator: ", CCIdLoc )

output:

other Course ID: 1577  //missing 0's here
locator : //div[contains(text(),'1577')]

Please let me know is there any other way to handle this. I wanted the locator to be //div[contains(text(),'PR001-001577')]

Thanks in Advance !

Upvotes: 0

Views: 343

Answers (3)

Green 绿色
Green 绿色

Reputation: 2886

As an intermediate step, you can use regular expressions to find and extract the leading zeros, save them into an additional variable (optional) and add them to the new number after you did your math operation.

However, you have to account for the special case when the number of leading zeros changes after the math operations (e.g., 1000-1=999).

let courseID = "PR001-CC001578";
let currCourseID = courseID.split('CC');
let leadingZeros = currCourseID[1].match(/^0*/); // changed this
let otherCourseID = leadingZeros + (currCourseID[1] - 1); // and changed this
if (otherCourseID.length < currCourseID[1].length) {
    otherCourseID = "0" + otherCourseID;
}
console.info("other Course ID:", otherCourseID);
var courseIDAssetsPg="//div[contains(text(),'%d')]";
var replaceCCId = courseIDAssetsPg.replace("%d", otherCourseID);
var CCIdLoc = element(by.xpath(replaceCCId));
console.info("locator: ", CCIdLoc )

Alternatively, you could simply pad the number with the appropriate number of leading zeros:

const numZeros = currCourseID[1].length - otherCourseID.toString().length;
otherCourseID = "0".repeat(numZeros) + otherCourseID;

Upvotes: 2

GT Electronics
GT Electronics

Reputation: 146

I think it would be easiest to parse the number out with RegEx, work with that number as you want (add or subtract 1), then assemble a new 6-digit number by adding enough leading zeros, then insert it into your string.

Upvotes: 0

aleks korovin
aleks korovin

Reputation: 744

I guess another way would be to use such approach with splitting id by two parts, changing the number and restoring the result number according to 6 digits format in that way:

let courseID = "PR001-CC001578";
const parts = courseID.split('-');
const lastNumber = parts[1].replace(/\D/g, "") - 1;
const formattedLastNumber = `${lastNumber}`.padStart(6, '0');

console.log(formattedLastNumber);

Upvotes: 4

Related Questions