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?