Ends With
Ends With - Explanation
Problem Summary
Write a function that checks if a string ends with a given suffix.
Go to Problem →Detailed Explanation
이 문제는 **endsWith() 메서드**로 문자열의 끝을 확인하는 방법을 학습합니다. ## 핵심 개념: 접미사 확인 endsWith()는 문자열이 특정 문자열로 끝나는지 확인합니다. ### 기본 사용법 ```javascript str.endsWith(searchString) str.endsWith(searchString, length) ``` - searchString: 찾을 접미사 - length: 검색할 문자열 길이 (기본값: str.length) ### 활용 예시 ```javascript "hello".endsWith("lo") // true "hello".endsWith("he") // false "hello".endsWith("ell", 4) // true (처음 4자에서) ``` ### 실무 활용 - 파일 확장자 확인: `filename.endsWith(".js")` - URL 경로 확인: `url.endsWith("/")` - 구두점 확인: `sentence.endsWith(".")` - 접미사 검증 ### startsWith와 쌍 ```javascript const url = "https://example.com/"; url.startsWith("https://") // 프로토콜 확인 url.endsWith("/") // 슬래시 확인 ``` 두 메서드를 함께 사용하면 문자열의 시작과 끝을 동시에 검증할 수 있습니다.
Solution Code
function endsWith(str, suffix) {
return str.endsWith(suffix);
}Key Concepts from This Problem
Common Mistakes
Hints
Complexity Analysis
Time Complexity
O(m)
Space Complexity
O(1)
Uses almost no additional memory