📜

Ends With

Ends With - Explanation

쉬움 문자열 O(m) O(1)

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

solution.js
function endsWith(str, suffix) {
  return str.endsWith(suffix);
}

Key Concepts from This Problem

1. endsWith 메서드
2. 접미사 확인
3. 파일 확장자 검사
4. ES6 메서드

Common Mistakes

대소문자를 구분합니다 - ".JS"와 ".js"는 다릅니다
빈 문자열로 확인하면 항상 true입니다
두 번째 매개변수는 endIndex가 아니라 length입니다

Hints

Hint 1: endsWith 메서드를 사용하세요.

Complexity Analysis

Time Complexity

O(m)

Space Complexity

O(1)

Uses almost no additional memory

Related Tags

#문자열 #endsWith #검사