📜

Trim String

Trim String - Explanation

쉬움 문자열 O(n) O(n)

Problem Summary

Write a function that removes whitespace from both ends of a string.

Go to Problem →

Detailed Explanation

이 문제는 **trim() 메서드**로 문자열의 양 끝 공백을 제거하는 방법을 학습합니다. ## 핵심 개념: 공백 제거 trim()은 문자열의 시작과 끝에서 공백 문자를 제거합니다. ### 기본 사용법 ```javascript str.trim() ``` - 원본 문자열은 변경되지 않습니다 - 양쪽 끝의 공백만 제거 (중간 공백은 유지) ### 관련 메서드들 ```javascript " hello ".trim() // "hello" " hello ".trimStart() // "hello " " hello ".trimEnd() // " hello" ``` ### 제거되는 문자 - 공백 (space) - 탭 (\t) - 줄바꿈 (\n, \r) - 기타 공백 문자 ### 실무 활용 - 사용자 입력 정리 - 폼 데이터 검증 - 파일에서 읽은 데이터 처리 - API 응답 정리 ```javascript const email = " user@email.com ".trim(); // "user@email.com" ``` 사용자 입력을 처리할 때는 항상 trim()을 고려하세요.

Solution Code

solution.js
function trimString(str) {
  return str.trim();
}

Key Concepts from This Problem

1. trim 메서드
2. 공백 제거
3. trimStart
4. trimEnd

Common Mistakes

중간 공백은 제거되지 않습니다
원본 문자열은 변경되지 않습니다
공백만 있는 문자열은 빈 문자열이 됩니다

Hints

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

Complexity Analysis

Time Complexity

O(n)

Grows linearly with input size

Space Complexity

O(n)

Uses memory proportional to input size

Related Tags

#문자열 #trim #공백