๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Javascript

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ : ๋ฌธ์ž์—ด ๋ณ€๊ฒฝ ๋ฉ”์„œ๋“œ : padStart() / padEnd()

by ์ฝ”ํŒŒ์นด 2022. 8. 17.
728x90

๋ฌธ์ž์—ด ๋ณ€๊ฒฝ ๋ฉ”์„œ๋“œ : padStart() / padEnd()

padStart() / padEnd() ๋ฉ”์„œ๋“œ๋Š” ์ฃผ์–ด์ง„ ๊ธธ์ด์— ๋งž๊ฒŒ ์•ž / ๋’ค ๋ฌธ์ž์—ด์„ ์ฑ„์šฐ๊ณ , ์ƒˆ๋กœ์šด ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.


padStart() ๋ฉ”์„œ๋“œ

padStart() ๋ฉ”์„œ๋“œ๋Š” ํ˜„์žฌ ๋ฌธ์ž์—ด์˜ ์‹œ์ž‘์„ ๋‹ค๋ฅธ ๋ฌธ์ž์—ด๋กœ ์ฑ„์›Œ, ์ฃผ์–ด์ง„ ๊ธธ์ด๋ฅผ ๋งŒ์กฑํ•˜๋Š” ์ƒˆ๋กœ์šด ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ฑ„์›Œ๋„ฃ๊ธฐ๋Š” ๋Œ€์ƒ ๋ฌธ์ž์—ด์˜ ์‹œ์ž‘(์ขŒ์ธก)๋ถ€ํ„ฐ ์ ์šฉ๋ฉ๋‹ˆ๋‹ค.

const str1 = "456";
const currentStr1 = str1.padStart(1, "0"); // ํ˜„์žฌ ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋ณด๋‹ค ์ž‘๋‹ค๋ฉด, ์ฑ„์›Œ๋„ฃ์ง€ ์•Š๊ณ  ๊ทธ๋Œ€๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
const currentStr2 = str1.padStart(2, "0");
const currentStr3 = str1.padStart(3, "0"); 
const currentStr4 = str1.padStart(4, "0"); 
const currentStr5 = str1.padStart(5, "0"); 
const currentStr6 = str1.padStart(6, "0"); 
const currentStr7 = str1.padStart(6, "1"); 
const currentStr8 = str1.padStart(6, "12"); 
const currentStr9 = str1.padStart(6, "123"); 
const currentStr10 = str1.padStart(6, "1239"); 
const currentStr11 = str1.padStart(6); // ๋ฌธ์ž์—ด์„ ์ž‘์„ฑํ•˜์ง€ ์•Š์œผ๋ฉด, ๋‚จ๋Š” ๊ธธ์ด๋งŒํผ ๊ณต๋ฐฑ ์ฒ˜๋ฆฌ๋ฉ๋‹ˆ๋‹ค.
๊ฒฐ๊ณผ๋ณด๊ธฐ
456
456
456
0456
00456
000456
111456
121456
123456
123456
___456

padEnd() ๋ฉ”์„œ๋“œ

padEnd() ๋ฉ”์„œ๋“œ๋Š” ํ˜„์žฌ ๋ฌธ์ž์—ด์˜ ๋์„ ๋‹ค๋ฅธ ๋ฌธ์ž์—ด๋กœ ์ฑ„์›Œ, ์ฃผ์–ด์ง„ ๊ธธ์ด๋ฅผ ๋งŒ์กฑํ•˜๋Š” ์ƒˆ๋กœ์šด ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ฑ„์›Œ๋„ฃ๊ธฐ๋Š” ๋Œ€์ƒ ๋ฌธ์ž์—ด์˜ ๋(์šฐ์ธก)๋ถ€ํ„ฐ ์ ์šฉ๋ฉ๋‹ˆ๋‹ค.

const str1 = "456";
const currentStr1 = str1.padEnd(1, "0");
const currentStr2 = str1.padEnd(2, "0"); 
const currentStr3 = str1.padEnd(3, "0");
const currentStr4 = str1.padEnd(4, "0"); 
const currentStr5 = str1.padEnd(5, "0"); 
const currentStr6 = str1.padEnd(6, "0"); 
const currentStr7 = str1.padEnd(6, "1"); 
const currentStr8 = str1.padEnd(6, "12"); 
const currentStr9 = str1.padEnd(6, "123"); 
const currentStr10 = str1.padEnd(6, "1239"); 
const currentStr11 = str1.padEnd(6);
๊ฒฐ๊ณผ๋ณด๊ธฐ
456
456
456
4560
45600
456000
456111
456121
456123
456123
456___

728x90

๋Œ“๊ธ€

๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค. ๐Ÿฆ™

CSS
๊ด‘๊ณ  ์ค€๋น„์ค‘