在 JavaScript 中从字符串中删除前 k 个字符
javascriptweb developmentfront end technologyobject oriented programming更新于 2026/1/25 23:08:17
我们需要编写一个 JavaScript 函数,该函数接受一个字符串和一个数字(假设为 k),并返回另一个从字符串中删除了前 k 个字符的字符串。
例如:如果原始字符串为 −
const str = "this is a string"
并且,
n = 4
那么输出应该是 −
const output = " is a string"
示例
其代码为 −
const str = 'this is a string';
const removeN = (str, num) => {
const { length } = str;
if(num > length){
return str;
};
const newStr = str.substr(num, length - num);
return newStr;
};
console.log(removeN(str, 3));
输出
控制台中的输出 −
s is a string
相关文章
有用资源
javascript 参考教程 - 该教程包含有关 javascript 的更多信息:https://www.cainiaomax.com/javascript/

