从开始和结束处删除 0 - JavaScript
javascriptweb developmentfront end technologyobject oriented programming更新于 2025/12/6 18:20:17
我们需要编写一个 JavaScript 函数,该函数将数字作为字符串接收,并返回删除所有前导 0 和尾随 0 的新数字字符串
例如:如果输入是 −
const strNum = '054954000'
那么输出应该是 −
const output = '54954'
示例
以下是代码 −
const strNum = '054954000';
const removeZero = (str = '') => {
const res = '';
let startLen = 0, endLen = str.length-1;
while(str[startLen] === '0'){
startLen++;
};
while(str[endLen] === '0'){
endLen--;
};
return str.substring(startLen, endLen+1);
};
console.log(removeZero(strNum));
输出
以下是控制台中的输出 −
54954
相关文章
有用资源
javascript 参考教程 - 该教程包含有关 javascript 的更多信息:https://www.cainiaomax.com/javascript/

