📜

Truncate String

Truncate String
쉬움 문자열 +10pts

Problem

Write a function that truncates a string to a given length and adds "..." if truncated.

Examples

Input: truncate("Hello World", 5)
Output: "Hello..."

Explanation

이 문제는 **조건부 문자열 처리**와 **텍스트 말줄임** 패턴을 학습합니다. UI에서 자주 사용되는 기능입니다. **알고리즘 동작** 1. 문자열 길이가 maxLength 이하면 그대로 반환 2. 그렇지 않으면 maxLength까지 자르고 "..." 추가 **"Hello World", maxLength=5 처리** - "Hello World".length (11) > 5 → 자르기 필요 - slice(0, 5) → "Hello" - "Hello" + "..." → "Hello..." **slice(0, maxLength)의...

View detailed explanation →

Key Concepts

slice 메서드 조건부 처리 문자열 연결 말줄임 패턴
Time: O(n) Space: O(n)
solution.js
Ctrl + Enter
Run tests to see results here.