如果第一個字串以特定第二個字串開頭,則返回 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
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP