IT_susu

String 객체의 새로운 메서드 본문

[ javascript ]/modern javaScript

String 객체의 새로운 메서드

고베베 2019. 1. 31. 10:46

includes()

텍스트가 포함되어있는지 여부를 불리언 값으로 반환.


(es5)

var word = "abcdefg";

word.indexOf('bcd') > -1


(es6)

let word = "abcdefg";

word.includes('bcd')


startsWith()

어떤 문자열이 특정 문자로 시작하는지 확인하여 불리언 값으로 반환.


(es5)

var kings_4 = '청룡 백호 현무 주작';


// 1. kings_4의 글자는 '백호'로 시작하는가?

kings_4.substr(0,2) === '백호';


function startsWith(word, find, start) {

start = start || 0;

return word.substr(start, find.length) === find;

}


startsWidth(kings_4, '주작', 9); // true


(es6)

let kings_4 = '청룡 백호 현무 주작';


// '현무'는 kings_4 글자의 6 인덱스부터 시작하는가?

kings_4.startsWidth('현무', 6);



endsWith()

어떤 문자열이 특정 문자로 끝나는지 확인하여 불리언 값으로 반환.


(es5)

var season = '봄 여름 가을 겨울';


var index = season.length - 2;

season.substr(index, 2) === '겨울' // true


function endsWith(word, find, end) {

end = (end || word.length) - find.length;

var last_index = word.indexOf(find, end);

return last_index !== -1 && last_index === end;

}


endsWith(season, '가을', 7); //true



(es6)

let season = '봄 여름 가을 겨울';


season.endsWith('겨울'); // true



repeat()

어떤 문자열을 특정한 개수만큼 반복하여 새로운 문자열을 반환.


(es5)

var repeat_word = '양심과 욕심 ';


function repeat(word, count) {

count = count || 0;

if ( count === 0 ) { return ' '; }

var combine = word;

while ( --count ) {

combine += word;

}

return combine;

}


repeat(repeat_word, 4); // 양심과 욕심 양심과 욕심 양심과 욕심 양심과 욕심


(es6)

let repeat_word = '양심과 욕심 ';

repeat_word.repeat(4)



'[ javascript ] > modern javaScript' 카테고리의 다른 글

비구조화 할당, 해체할당, 구조분해 destructuring assignment  (0) 2019.08.05
Arrow function  (0) 2019.08.05
gulp  (0) 2019.01.15
모듈  (0) 2019.01.15
spread operator  (0) 2018.11.06
Comments