truncate
Truncates a string to a specified length.
truncate
Signature
typescript
function truncate(str: string, length: number, suffix: string = "..."): stringParameters
str- The string to truncatelength- Maximum length (including suffix)suffix- String to append (default:"...")
Returns
Truncated string with suffix if needed.
Examples
typescript
import { truncate } from 'strio'
// Basic usage
truncate('Hello World', 8)
// "Hello..."
truncate('Short', 10)
// "Short" (no truncation needed)
// Custom suffix
truncate('Long text here', 10, '…')
// "Long text…"
// Empty suffix
truncate('Testing', 4, '')
// "Test"Use Cases
Preview Text
typescript
import { truncate } from 'strio'
function createPreview(text: string): string {
return truncate(text, 100)
}
createPreview('Very long article content...')
// "Very long article content..." (up to 100 chars)Filename Display
typescript
import { truncate } from 'strio'
function displayFilename(filename: string): string {
return truncate(filename, 20)
}
displayFilename('very-long-document-name.pdf')
// "very-long-docume..."Related Functions
- truncateWords - Truncate by word count
- words - Split into words