Last updated
Text Truncator Examples
The Text Truncator shortens text to a specified length while preserving readability and adding appropriate truncation indicators. Below are practical examples for meta descriptions, card titles, notifications, and database fields.
Character Limit Truncation
// Input (200 characters):
"The quick brown fox jumps over the lazy dog. This sentence is used to demonstrate text truncation at a specific character limit while preserving word boundaries."
// Truncate at 60 characters (word boundary):
"The quick brown fox jumps over the lazy dog. This..."
// Truncate at 100 characters:
"The quick brown fox jumps over the lazy dog. This sentence is used to demonstrate text truncation..."
// Truncate at 155 characters (meta description):
"The quick brown fox jumps over the lazy dog. This sentence is used to demonstrate text truncation at a specific character limit..."
Word Limit Truncation
// Input:
"The quick brown fox jumps over the lazy dog near the river bank on a sunny afternoon."
// Truncate at 5 words:
"The quick brown fox jumps..."
// Truncate at 10 words:
"The quick brown fox jumps over the lazy dog near..."
// Truncate at 15 words:
"The quick brown fox jumps over the lazy dog near the river bank on a..."
Sentence Limit Truncation
// Input (4 sentences):
"This is the first sentence. Here comes the second one. The third sentence adds more detail. Finally, the fourth sentence concludes the paragraph."
// Truncate at 1 sentence:
"This is the first sentence."
// Truncate at 2 sentences:
"This is the first sentence. Here comes the second one."
// Truncate at 3 sentences:
"This is the first sentence. Here comes the second one. The third sentence adds more detail."
Custom Truncation Indicators
// Standard ellipsis character (…)
"The quick brown fox jumps over…"
// Three periods (...)
"The quick brown fox jumps over..."
// [read more] indicator
"The quick brown fox jumps over [read more]"
// [continued] indicator
"The quick brown fox jumps over [continued]"
// No indicator (hard cut at word boundary)
"The quick brown fox jumps over"
SEO Meta Description Truncation
// Google displays ~155-160 characters in search results
// Input (too long):
"Learn how to build scalable REST APIs with Node.js and Express. This comprehensive guide covers authentication, rate limiting, error handling, database integration, and deployment best practices for production-ready APIs."
// Truncated to 155 characters:
"Learn how to build scalable REST APIs with Node.js and Express. This comprehensive guide covers authentication, rate limiting, error handling..."
// Character count: 155 ✓
// Ends at word boundary ✓
// Includes primary keywords ✓
Page Title Truncation
// Google displays ~50-60 characters in search results
// Input:
"The Complete Beginner's Guide to Machine Learning with Python and TensorFlow"
// Truncated to 60 characters:
"The Complete Beginner's Guide to Machine Learning with..."
// Better approach — rewrite to fit naturally:
"Beginner's Guide to Machine Learning with Python"
// Length: 48 characters ✓
Card Title Truncation (UI Components)
// Product card title (max 50 chars)
"Sony WH-1000XM5 Wireless Noise Canceling Headphones"
→ "Sony WH-1000XM5 Wireless Noise Canceling..."
// Blog post card (max 80 chars)
"10 Essential JavaScript Tips Every Developer Should Know in 2024"
→ "10 Essential JavaScript Tips Every Developer Should Know in 2024" (fits)
"The Ultimate Guide to Building Production-Ready Microservices with Docker and Kubernetes"
→ "The Ultimate Guide to Building Production-Ready Microservices with Docker..."
Notification Message Truncation
// Push notification (max 100 chars)
"You have a new message from Alice: Hey, are you free for a call this afternoon? I wanted to discuss the project timeline."
→ "You have a new message from Alice: Hey, are you free for a call this afternoon? I..."
// Toast notification (max 60 chars)
"Your file has been successfully uploaded to the server."
→ "Your file has been successfully uploaded to the server." (fits at 55 chars)
"Your file 'project-documentation-final-v3.pdf' has been successfully uploaded."
→ "Your file 'project-documentation-final-v3.pdf' has been..."
Database Field Truncation
// VARCHAR(255) field
"This is a very long description that exceeds the database field limit of 255 characters. It needs to be truncated before insertion to avoid a database error or data truncation warning from the database engine."
→ Truncated to 255 chars at word boundary
// VARCHAR(100) field
"Product description text that is longer than the allowed field width"
→ Truncated to 100 chars
// CHAR(10) fixed-width field (pad if shorter)
"Hello" → "Hello " (padded to 10)
"Hello World" → "Hello Worl" (truncated to 10)
HTML-Aware Truncation
// Input HTML:
"<p>This is <strong>important</strong> content with <a href='#'>a link</a> inside.</p>"
// Naive truncation (WRONG — breaks HTML):
"<p>This is <strong>important</strong> content with <a href='#'>a li..."
// Unclosed <a> tag!
// HTML-aware truncation (CORRECT):
"<p>This is <strong>important</strong> content with...</p>"
// All tags properly closed ✓
JavaScript Truncation Functions
// Character truncation (word boundary)
function truncateChars(text, limit, suffix = '...') {
if (text.length <= limit) return text;
const truncated = text.slice(0, limit - suffix.length);
return truncated.slice(0, truncated.lastIndexOf(' ')) + suffix;
}
// Word truncation
function truncateWords(text, wordLimit, suffix = '...') {
const words = text.split(/\s+/);
if (words.length <= wordLimit) return text;
return words.slice(0, wordLimit).join(' ') + suffix;
}
truncateChars("The quick brown fox", 15) // "The quick..."
truncateWords("The quick brown fox", 3) // "The quick brown..."
Platform Character Limits Reference
- Google meta description: 155–160 characters
- Google page title: 50–60 characters
- Twitter/X post: 280 characters
- LinkedIn post: 3,000 characters (preview: 210)
- Instagram caption: 2,200 characters (preview: 125)
- Push notification title: 50 characters
- Push notification body: 100 characters
- SMS: 160 characters (1 segment)
- Email subject line: 40–60 characters (preview)
Enter your text and target length in the Text Truncator to get intelligently truncated output that respects word boundaries and preserves readability.