startsWith
, endsWith, includes

Yeonkor·2020년 9월 14일
0

Javascript

목록 보기
9/12

String 기반 메소드들로써, 문자열 값을 판단하여 , 정렬 후 불러올때 용이하다. 불리언 값을 반환한다.

1.startWith()

startsWith() 메소드는 어떤 문자열이 특정 문자로 시작하는지 확인하여 결과를 true 혹은 false로 반환한다.

구문 : str.startsWith(searchString[, position])


//  startsWith

let wolves = `where can i watch the raised by wolves?`; // length : 39

console.log(wolves.startsWith('where can i'));  // true
console.log(wolves.startsWith('by'));     // false
console.log(wolves.startsWith('watch', 12)); // true , 공백도 간격으로 카운트 해야한다.

2.endsWith()

The endsWith() 메서드를 사용하여 어떤 문자열에서 특정 문자열로 끝나는지를 확인할 수 있으며, 그 결과를 true 혹은 false로 반환한다.

구문 : str.endsWith(searchString[, length])


//  endsWith

let wolves = `where can i watch the raised by wolves?`; // length : 39

console.log(wolves.endsWith('wolves?'));  // true
console.log(wolves.endsWith('i'));     // false
console.log(wolves.endsWith('raised', 28)); // true , raised의 끝나는 자릿수

3.includes()

includes() 메서드는 하나의 문자열이 다른 문자열에 포함되어 있는지를 판별하고, 결과를 true 또는 false 로 반환한다.

구문 : str.includes(searchString[, position])


//  includes

let wolves = `where can i watch the raised by wolves?`; // length : 39

var str = 'To be, or not to be, that is the question.';

console.log(wolves.includes('i watch the'));       // true
console.log(wolves.includes('wolves?'));    // true
console.log(wolves.includes('where Can i')); // false , 메소드가 대소문자를 구분함
console.log(wolves.includes('watch the', 1));    // false , 시작점이 맞지 않음
console.log(wolves.includes('By'));       // false , 대소문자 구분



profile
CTO를 꿈꾸는 CDO

0개의 댓글