简体   繁体   English

如何从字符串中减去和保留前导零

[英]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.我必须从字符串PR001-CC001578中减去 -1 并将其作为参数传递给xpath以识别元素。 Am splitting it with CC and subtracting -1 from 001578 .我用CC拆分它并从001578中减去 -1 。 The result is 1577 .结果是1577 But the leading zeros are removed because of which the xpath identification is failing.但前导零被删除,因为xpath识别失败。

        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: 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')]我希望定位器是//div[contains(text(),'PR001-001577')]

Thanks in Advance !提前致谢 !

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:我想另一种方法是使用这种方法将 id 分为两部分,以这种方式更改数字并根据 6 位格式恢复结果编号:

 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);

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 (eg, 1000-1=999).但是,您必须考虑在数学运算后前导零的数量发生变化的特殊情况(例如,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;

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.我认为用 RegEx 解析数字是最简单的,根据需要使用该数字(加或减 1),然后通过添加足够的前导零来组装一个新的 6 位数字,然后将其插入到您的字符串中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM