728x90
padStart() / padEnd()
padStart()/padEnd() 메서드는 주어진 길이에 맞게 앞/뒤 문자열을 채우고, 새로운 문자열을 반환합니다.
"문자열".padStart(길이)
"문자열".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, "1234"); //자릿수 안 맞아서 '4'가 잘림
const currentStr11 = str1.padStart(6); //앞에 공백으로 처리
const currentStr12 = str1.padEnd(1, "0");
const currentStr13 = str1.padEnd(2, "0");
const currentStr14 = str1.padEnd(3, "0");
const currentStr15 = str1.padEnd(4, "0");
const currentStr16 = str1.padEnd(5, "0");
const currentStr17 = str1.padEnd(6, "0");
const currentStr18 = str1.padEnd(6, "1");
const currentStr19 = str1.padEnd(6, "12");
const currentStr20 = str1.padEnd(6, "123");
const currentStr21 = str1.padEnd(6, "1234");
const currentStr22 = str1.padEnd(6); //뒤에 공백으로 처리
결과보기
456
456
456
0456
00456
000456
111456
121456
123456
123456
456
456
456
456
4560
45600
456000
456111
456121
456123
456123
456
456
456
0456
00456
000456
111456
121456
123456
123456
456
456
456
456
4560
45600
456000
456111
456121
456123
456123
456
728x90
반응형
'Javascript' 카테고리의 다른 글
[javascript]함수 유형 (3) | 2022.08.22 |
---|---|
[javascript]includes() (3) | 2022.08.17 |
[javascript]repeat() (3) | 2022.08.17 |
[javascript]concat() (3) | 2022.08.17 |
[javascript]replace() / replaceAll() (3) | 2022.08.17 |
댓글