📜

Starts With

Starts With - Explanation

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

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

solution.js
function startsWith(str, prefix) {
  return str.startsWith(prefix);
}

Key Concepts from This Problem

1. startsWith 메서드
2. 접두사 확인
3. 문자열 검사
4. ES6 메서드

Common Mistakes

대소문자를 구분합니다 - 필요시 toLowerCase()와 함께 사용하세요
빈 문자열로 확인하면 항상 true입니다
정규표현식은 사용할 수 없습니다

Hints

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

Complexity Analysis

Time Complexity

O(m)

Space Complexity

O(1)

Uses almost no additional memory

Related Tags

#문자열 #startsWith #검사