텍스트 자르기 도구
사용자 정의 말줄임표로 지정된 글자 수 또는 단어 수로 텍스트 자르기.
자주 묻는 질문
코드 구현
def truncate_chars(text: str, max_chars: int, ellipsis: str = "...") -> str:
"""Truncate text to max_chars characters."""
if len(text) <= max_chars:
return text
return text[:max_chars - len(ellipsis)] + ellipsis
def truncate_words(text: str, max_words: int, ellipsis: str = "...") -> str:
"""Truncate text to max_words words."""
words = text.split()
if len(words) <= max_words:
return text
return " ".join(words[:max_words]) + ellipsis
# Examples
text = "The quick brown fox jumps over the lazy dog"
print(truncate_chars(text, 20)) # "The quick brown fo..."
print(truncate_words(text, 5)) # "The quick brown fox jumps..."
print(truncate_chars(text, 20, " …")) # "The quick brown f …"Comments & Feedback
Comments are powered by Giscus. Sign in with GitHub to leave a comment.