如果第一個字串以特定第二個字串開頭,則返回 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.