본문 바로가기
Javascript

문자열 메서드 : padStart() / padEnd()

by 코딩 척척학사 2022. 8. 17.
728x90

문자열 메서드 : padStart() / padEnd()

문자열 메서드를 활용하면 앞/뒤 문자열에 새로운 문자를 추가하며 자릿수를 채울 수 있습니다.

> padStart() / padEnd() 메서드

padStart()/padEnd() 메서드는 주어진 길이에 맞게 앞/뒤 문자열을 채우고, 새로운 문자열을 반환합니다.

"문자열".padStart(길이)
"문자열".padStart(길이, 문자열)
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 //문자열 자리에 아무것도 안쓰면 공백처리

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___
728x90

'Javascript' 카테고리의 다른 글

문자열 메서드 : search()  (7) 2022.08.22
문자열 메서드 : includes()  (6) 2022.08.17
문자열 메서드 : repeat()  (5) 2022.08.17
문자열 메서드 : concat()  (4) 2022.08.17
문자열 메서드 : replace() / replaceAll()  (3) 2022.08.17

댓글


HTML이 적힌 썸네일 이미지
CSS가 적힌 썸네일 이미지
JAVASCRIPT가 적힌 썸네일 이미지

JAVASCRIPT

자세히 보기