console.log('a+very+nice+string'.split('+')); // ['a ', ' very ', ' nice ', ' string']
console.log('Bong Kim'.split(' ')); // ['Bong', 'Kim']
const [firstName, lastName] = 'Bong Kim'.split(' ');
const newName = ['Mr.', firstName, lastName.toUpperCase()].join(' ');
console.log(newName); // Mr. Bong KIM
const news = ['Mr', 'Bong', 'Kim'].join('.');
console.log(news); // Mr.Bong.Kim
const newNames = ['Mr'].join('.');
console.log(newNames); // Mr
const capitalizeName = function (name) {
const names = name.split(' ');
const namesUpper = [];
for (const n of names) {
namesUpper.push(n.replace(n[0], n[0].toUpperCase()));
}
console.log(namesUpper.join(' ')); // Park And Lee, Bong Kim
};
capitalizeName('park and lee');
capitalizeName('bong kim');
split(A) - A를 기준으로 자른 후 배열 형태로 반환.
join(A) - 문자열 사이에 A를 추가한 후 반환. (변수에 저장된 문자열에서 하나의 문자열만 존재하면 변수 그대로 반환.)
const message = 'Go to gate 23!';
console.log(message.padStart(15, '+')); // +Go to gate 23!
console.log(message.padEnd(15, '+')); // Go to gate 23!+
console.log('Bongo'.padStart(10, '+').padEnd(15, '+')); // +++++Bongo+++++
console.log('Bongo'.padEnd(15, '+').padStart(10, '+')); // Bongo++++++++++
const message2 = 'Bad weather... ';
console.log(message2.repeat(5));
// Bad weather... Bad weather... Bad weather... Bad weather... Bad weather...
const planesInLine = function (n) {
console.log(`There are ${n} planes in line ${'Airplane '.repeat(n)}`);
};
planesInLine(3); // There are 5 planes in line Airplane Airplane Airplane
planesInLine(5); // There are 5 planes in line Airplane Airplane Airplane Airplane Airplane
planesInLine(10); // There are 5 planes in line Airplane *10
padStart(A, B) - 변수에 저장된 문자열에서 총 길이 A만큼 B를 앞에서부터 채워서 반환.
(변수에 저장된 문자열의 길이가 A보타 크면 변수 그대로 반환.)
padEnd(A, B) - 변수에 저장된 문자열에서 총 길이 A만큼 B를 뒤에서부터 채워서 반환.
(변수에 저장된 문자열의 길이가 A보타 크면 변수 그대로 반환.)
repeat(A) - 변수에 저장된 문자열을 A만큼 반복하여 반환.
'JavaScript' 카테고리의 다른 글
JavaScript 배열 (0) | 2022.04.05 |
---|---|
JavaScript 함수 (0) | 2022.04.05 |
JavaScript 예시로 정리 (0) | 2022.04.04 |
연산자 (0) | 2022.04.02 |
(배열,객체) 구조화, 스프레드 연산자 (0) | 2022.04.02 |
댓글