Starts With
Starts With - Explanation
Problem Summary
Write a function that checks if a string starts with a given prefix.
Go to Problem →Detailed Explanation
이 문제는 **startsWith() 메서드**로 문자열의 시작을 확인하는 방법을 학습합니다. ## 핵심 개념: 접두사 확인 startsWith()는 문자열이 특정 문자열로 시작하는지 확인합니다. ### 기본 사용법 ```javascript str.startsWith(searchString) str.startsWith(searchString, position) ``` - searchString: 찾을 접두사 - position: 검색 시작 위치 (기본값 0) ### 활용 예시 ```javascript "hello".startsWith("he") // true "hello".startsWith("lo") // false "hello".startsWith("ell", 1) // true (1번 인덱스부터) ``` ### 실무 활용 - URL 프로토콜 확인: `url.startsWith("https://")` - 파일 경로 검증: `path.startsWith("/")` - 명령어 파싱: `command.startsWith("/")` - 검색 자동완성 ### indexOf와 비교 ```javascript // 이전 방식 str.indexOf(prefix) === 0 // 현대적 방식 (권장) str.startsWith(prefix) ``` startsWith()가 더 명확하고 읽기 쉽습니다.
Solution Code
function startsWith(str, prefix) {
return str.startsWith(prefix);
}Key Concepts from This Problem
Common Mistakes
Hints
Complexity Analysis
Time Complexity
O(m)
Space Complexity
O(1)
Uses almost no additional memory