โญ๏ธ ๋ฌธ์์ด ๊ฐ์ฒด ( padStart / padEnd ๋ฉ์๋ ) โญ๏ธ
01. ํ์ค ๋ด์ฅ ๊ฐ์ฒด์ ์ข ๋ฅ : ๋ฌธ์์ด ๊ฐ์ฒด
ํ์ค ๋ด์ฅ ๊ฐ์ฒด(Standard Built-in Object)๋ ์๋ฐ์คํฌ๋ฆฝํธ๊ฐ ๊ธฐ๋ณธ์ ์ผ๋ก ๊ฐ์ง๊ณ ์๋ ๊ฐ์ฒด๋ค์ ๋งํ๋ฉฐ, ๋ค๋ฅธ ๊ฐ์ฒด์ ๊ธฐ์ด๊ฐ ๋๋ ํต์ฌ์ ์ธ ๊ฐ์ฒด ์
๋๋ค.
๋ด์ฅ ๊ฐ์ฒด์ ์ข
๋ฅ์๋ Object, Fuction, String, Array, Math, Number, Event, Boolean, Data, RegExp ๋ฑ์ด ์์ต๋๋ค.
๊ทธ ์ค์์ ๋ฌธ์์ด ๊ฐ์ฒด(String Object)๋ ๋ฌธ์์ด์ ์ ์ฅ/๊ด๋ฆฌํ๊ธฐ ์ํด ์ฌ์ฉํ๋ ๊ฐ์ฒด๋ฅผ ์๋ฏธํฉ๋๋ค.
___ ์ฐธ๊ณ ํ๊ธฐ ___
`1 ๋ฌธ์์ด์ ์์ฑํ ๋๋ ''"" ๋ฐ์ดํ๋ฅผ ์ฌ์ฉํ๊ฑฐ๋, new ํค์๋๋ฅผ ์ด์ฉํฉ๋๋ค.
`2 ๋ฌธ์์ด ๊ฐ์ฒด๋ ๋ถ๋ณ์ฑ์ด ์์ผ๋ฉฐ, ์ด๋ก์จ ๋ฉ๋ชจ๋ฆฌ ๊ณต๊ฐ์ ์ ์ฝ๊ณผ ๋ณด์์ฑ ๋ฐ ๋์์ฑ์ ํน์ง๋ ํจ๊ป ๊ฐ์ต๋๋ค.
02. padStart / padEnd ๋ฉ์๋
์ฃผ์ด์ง ๊ธธ์ด์ ๋ง์ถฐ ๋ฌธ์์ด์ ์ฑ์์ ๋ฐํํ๋ ๋ฉ์๋ ์
๋๋ค.
๐ข padStart( ) ๋ฉ์๋๋ ์ฃผ์ด์ง ๊ธธ์ด์ ๋ง์ถฐ ๋ฌธ์์ด์ ์์ ๋ถ๋ถ์ ์ฑ์์ ๋ฐํํฉ๋๋ค.
๐ข padEnd( ) ๋ฉ์๋๋ ์ฃผ์ด์ง ๊ธธ์ด์ ๋ง์ถฐ ๋ฌธ์์ด์ ๋ ๋ถ๋ถ์ ์ฑ์์ ๋ฐํํฉ๋๋ค.
[1] padStart( ) ์ padEnd( ) ๋ฉ์๋์ ํ์
"๋ฌธ์์ด".padStart(๊ธธ์ด ๊ฐ, ์ฑ์์ค ๋ฌธ์์ด);
// padStart( ) ๋ฉ์๋์ ๋ฆฌํด ----------------------------------------------------------------
const str1 = "678";
const currentStr1 = str1.padStart(1, "0"); // 678
const currentStr2 = str1.padStart(2, "0"); // 678
const currentStr3 = str1.padStart(3, "0"); // 678
const currentStr4 = str1.padStart(4, "0"); // 0678
const currentStr5 = str1.padStart(5, "0"); // 00678
const currentStr6 = str1.padStart(6, "0"); // 000678
const currentStr7 = str1.padStart(6, "1"); // 111678
const currentStr8 = str1.padStart(6, "12"); // 121678
const currentStr9 = str1.padStart(6, "123"); // 123678
const currentStr10 = str1.padStart(6, "1234"); // 123678
const currentStr11 = str1.padStart(6); // ---678 (---: ๊ณต๋ฐฑ)
// padEnd( ) ๋ฉ์๋์ ๋ฆฌํด ----------------------------------------------------------------
const str1 = "678";
const currentStr12 = str1.padEnd(1, "0"); // 678
const currentStr13 = str1.padEnd(2, "0"); // 678
const currentStr14 = str1.padEnd(3, "0"); // 678
const currentStr15 = str1.padEnd(4, "0"); // 6780
const currentStr16 = str1.padEnd(5, "0"); // 67800
const currentStr17 = str1.padEnd(6, "0"); // 678000
const currentStr18 = str1.padEnd(6, "1"); // 678111
const currentStr19 = str1.padEnd(6, "12"); // 678121
const currentStr20 = str1.padEnd(6, "123"); // 678123
const currentStr21 = str1.padEnd(6, "1234"); // 678123
const currentStr22 = str1.padEnd(6); // 678--- (---: ๊ณต๋ฐฑ)
'Javascript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๋ฌธ์์ด ๊ฐ์ฒด (9) includes ๋ฉ์๋ (3) | 2022.08.18 |
---|---|
๋ฌธ์์ด ๊ฐ์ฒด (8) indexOf / lastindexOf ๋ฉ์๋ (2) | 2022.08.18 |
๋ฌธ์์ด ๊ฐ์ฒด (6) concat / repeat ๋ฉ์๋ (2) | 2022.08.18 |
๋ฌธ์์ด ๊ฐ์ฒด (5) replace / replaceAll ๋ฉ์๋ (2) | 2022.08.18 |
๋ฌธ์์ด ๊ฐ์ฒด (4) split ๋ฉ์๋ (2) | 2022.08.18 |
๋๊ธ