如果第一个字符串以特定第二个字符串开头,则返回 TRUE JavaScript


我们需要编写一个 JavaScript 函数,该函数接收两个字符串,并检查第一个字符串是否以第二个字符串开头

例如 −

If the two strings are:
“Disaster management report”
“Disas”
Then our function should return true

让我们编写此函数的代码 −

示例

const first = 'The game is on';
const second = 'The';
const startsWith = (first, second) => {
   const { length } = second;
   const { length: l } = first;
   const sub = first.substr(0, length);
   return sub === second;
};
console.log(startsWith(first, second));

输出

控制台中的输出将为 −

true

更新于: 31-Aug-2020

157 次浏览

启动你的职业

完成课程获得认证

开始
广告
© . All rights reserved.