자바스크립트에서 특정값으로 끝나는지
아니면 특정값으로 시작하는지 확인할 수 있는 메서드가 있습니다.
| endsWith()
endsWidth는 특정값으로 끝나는지 확인할 수 있는 메서드입니다.
1. 문법
endsWidth() 메서드는 매개변수 2가지를 집어넣을 수 있습니다.
string.endsWith(searchString, length)
여기서 serchString은 확인하고자 하는 특정 문자열, length는 찾으려고 하는 문자열의 길이입니다.
length는 안 넣어도 되고 기본값은 문자열 전체 길이의 값입니다.
2. 사용
console.log("apple".endsWith("e")); // apple이 e로 끝나므로 true
console.log("apple".endsWith("l")); // apple이 l로 끝나지 않으므로 false
console.log("apple".endsWith("p", 3)); // app가 p로 끝나므로 true
| startsWith()
endsWidth는 특정값으로 시작하는지 확인할 수 있는 메서드입니다.
1. 문법
startsWidth() 메서드는 매개변수 2가지를 집어넣을 수 있습니다.
string.startsWith(searchString, index);
여기서 serchString은 확인하고자 하는 특정 문자열, index는 탐색을 시작할 index 값입니다.
index는 안 넣어도 되고 기본값은 0입니다.
3. 사용
console.log("apple".startsWith("a")); // apple이 a로 시작하므로 true
console.log("apple".startsWith("p")); // apple이 p로 시작하지않으므로 false
console.log("apple".startsWith("p", 2)); // ple가 p로 시작하므로 true
글이 도움됐나요?
