๋ฐ์ํ
๋ฌธ์์ด ๋ฉ์๋ : padStart() / padEnd()
์ด๋ฒ ํฌ์คํ ์์๋ ๋ฌธ์์ด ๋ฉ์๋ ์ค padStart()์ padEnd()์ ๋ํด ๋ค๋ค๋ณด๋๋ก ํ๊ฒ ์ต๋๋ค.
padStart() / padEnd()
์ฃผ์ด์ง ๊ธธ์ด์ ๋ง๊ฒ ์ยท๋ค์ ๋ฌธ์์ด์ ์ฑ์ฐ๊ณ , ์๋ก์ด ๋ฌธ์์ด์ ๋ฐํํ๋ค.
์์ ๋ฌธ์์ด์ ์ฑ์ฐ๋ ๊ฑด padStart(), ๋ค์ ๋ฌธ์์ด์ ์ฑ์ฐ๋ ๊ฑด padEnd().
"๋ฌธ์์ด".padStart(๊ธธ์ด)
"๋ฌธ์์ด".padStart(๊ธธ์ด, ์ฑ์ธ ๋ฌธ์์ด)
"๋ฌธ์์ด".padEnd(๊ธธ์ด)
"๋ฌธ์์ด".padEnd(๊ธธ์ด, ์ฑ์ธ ๋ฌธ์์ด)
์ฝ๋ ๋ณด๊ธฐ
const str1 = "456";
const currentStr1 = str1.padStart(1, "0"); // 456
const currentStr2 = str1.padStart(2, "0"); // 456
const currentStr3 = str1.padStart(3, "0"); // 456
const currentStr4 = str1.padStart(4, "0"); // 0456
const currentStr5 = str1.padStart(5, "0"); // 00456
const currentStr6 = str1.padStart(6, "0"); // 000456
const currentStr7 = str1.padStart(6, "1"); // 111456
const currentStr8 = str1.padStart(6, "12"); // 121456
const currentStr9 = str1.padStart(6, "123"); // 123456
const currentStr10 = str1.padStart(6, "1234"); // 123456
const currentStr11 = str1.padStart(6); // ___456 : ๊ณต๋ฐฑ3์นธ + 456 ์ถ๋ ฅ
const currentStr12 = str1.padEnd(1, "0"); // 456
const currentStr13 = str1.padEnd(2, "0"); // 456
const currentStr14 = str1.padEnd(3, "0"); // 456
const currentStr15 = str1.padEnd(4, "0"); // 4560
const currentStr16 = str1.padEnd(5, "0"); // 45600
const currentStr17 = str1.padEnd(6, "0"); // 456000
const currentStr18 = str1.padEnd(6, "1"); // 456111
const currentStr19 = str1.padEnd(6, "12"); // 456121
const currentStr20 = str1.padEnd(6, "123"); // 456123
const currentStr21 = str1.padEnd(6, "1234"); // 456123
const currentStr22 = str1.padEnd(6); // 456___ : 456 + ๊ณต๋ฐฑ3์นธ
๋ฐ์ํ
'JavaScript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JavaScript] ํจ์ ์ ํ ์์๋ณด๊ธฐ (6) | 2022.08.22 |
---|---|
[JavaScript] ๋ฌธ์์ด ๋ฉ์๋ - includes() (4) | 2022.08.17 |
[JavaScript] ๋ฌธ์์ด ๋ฉ์๋ - repeat() (3) | 2022.08.17 |
[JavaScript] ๋ฌธ์์ด ๋ฉ์๋ - concat() (2) | 2022.08.17 |
[JavaScript] ๋ฌธ์์ด ๋ฉ์๋ - replace() & replaceAll() (3) | 2022.08.17 |
๋๊ธ