string.startsWith
startWith()
๋ฉ์๋๋ ์ด๋ค ๋ฌธ์์ด์ด ํน์ ๋ฌธ์๋ก ์์ํ๋์ง ํ์ธํ์ฌ ๊ฒฐ๊ณผ๋ฅผ true
๋๋ false
๋ก ๋ฐํํ๋ค.
Syntax
str.startsWith(searchString[, postion])
Parameters
searchStarting
์ด ๋ฌธ์์ด์ ์์ ๋ถ๋ถ์์ ๊ฒ์ํ ๋ฌธ์
position (Optional)
๋ฌธ์์ด์์ searchString
๊ฒ์์ ์์ํ ์์น์ด๋ค. ๊ธฐ๋ณธ๊ฐ์ 0์ด๋ค.
Return value
์ฃผ์ด์ง ๋ฌธ์์ด์ด ๋ฌธ์๊ฐ ๋ฌธ์์ด์ ์์ ๋ถ๋ถ์์ ๋ฐ๊ฒฌ๋๋ฉด true
, ๊ทธ๋ ์ง ์์ผ๋ฉด false
Description
์ด ๋ฌธ์์ด์ด ๋ค๋ฅธ ๋ฌธ์์ด๋ก ์์ํ๋์ง ํ์ธํ ์ ์๋ค. ๋์๋ฌธ์๋ฅผ ๊ตฌ๋ถํ๋ค.
Examples
startsWith() ์ฌ์ฉํ๊ธฐ
const str = 'Domestic cats, especially young kittens, are known for their love of play.';
str.startsWith('Domestic cats'); // true
str.startsWith('cats'); // false
str.startsWith('cats', 9); // true
Polyfill
startsWith ๋ฉ์๋๋ ECMAScript 2015 ๋ช ์ธ์ ์ถ๊ฐ๋์๋ค.
if (!String.prototype.startsWith) {
Object.defineProperty(String.prototype, 'startsWith', {
value: function(search, rawPos) {
var pos = rawPos > 0 ? rawPos|0 : 0;
return this.substring(pos, pos + search.length) === search;
}
});
}
ES2015 ๊ท๊ฒฉ ์ค์์ ๊ฒฌ๊ณ ํ์ง๋ง ํผํฌ๋จผ์ค๋ ๋จ์ด์ง๋ Polyfill์ Mathias Bynens์ GitHub ์์ ํ์ธํ ์ ์๋ค.
Last updated
Was this helpful?