String Truncation
October 21, 2025 · View on GitHub
Humanizer provides intelligent string truncation with multiple strategies to handle different use cases.
Overview
Truncation is useful when you need to limit string length for display purposes while maintaining readability. Humanizer offers several truncation strategies and uses the … character (one character) instead of "..." (three characters) to maximize visible text.
Basic Usage
using Humanizer;
"Long text to truncate".Truncate(10)
// => "Long text…"
"Long text to truncate".Truncate(10, "---")
// => "Long te---"
Truncation Strategies
Fixed Length (Default)
Truncates to a specific total length including the truncation indicator:
"Long text to truncate".Truncate(10, Truncator.FixedLength)
// => "Long text…"
"Long text to truncate".Truncate(10, "---", Truncator.FixedLength)
// => "Long te---"
Fixed Number of Characters
Truncates to a specific number of alphanumeric characters:
"Long text to truncate".Truncate(6, Truncator.FixedNumberOfCharacters)
// => "Long t…"
"Long text to truncate".Truncate(6, "---", Truncator.FixedNumberOfCharacters)
// => "Lon---"
Fixed Number of Words
Truncates to a specific number of words:
"Long text to truncate".Truncate(2, Truncator.FixedNumberOfWords)
// => "Long text…"
"Long text to truncate".Truncate(2, "---", Truncator.FixedNumberOfWords)
// => "Long text---"
Dynamic Length - Preserve Words
Similar to fixed length but attempts to preserve whole words:
"Long text to truncate".Truncate(10, Truncator.DynamicLengthAndPreserveWords)
// => "Long text…"
"Long text to truncate".Truncate(10, "---", Truncator.DynamicLengthAndPreserveWords)
// => "Long---"
Dynamic Characters - Preserve Words
Similar to fixed number of characters but attempts to preserve whole words:
"Long text to truncate".Truncate(6, Truncator.DynamicNumberOfCharactersAndPreserveWords)
// => "Long…"
"Long text to truncate".Truncate(6, "---", Truncator.DynamicNumberOfCharactersAndPreserveWords)
// => "---"
Truncation Direction
By default, truncation happens from the right (end) of the string. You can truncate from the left (beginning) using TruncateFrom.Left:
"Long text to truncate".Truncate(10, Truncator.FixedLength, TruncateFrom.Left)
// => "… truncate"
"Long text to truncate".Truncate(10, "---", Truncator.FixedLength, TruncateFrom.Left)
// => "---runcate"
"Long text to truncate".Truncate(2, Truncator.FixedNumberOfWords, TruncateFrom.Left)
// => "…to truncate"
Custom Truncation Indicators
You can use any string as a truncation indicator:
"Long text to truncate".Truncate(15, " [more]")
// => "Long [more]"
"Long text to truncate".Truncate(15, "...")
// => "Long text..."
Custom Truncators
Implement the ITruncator interface to create custom truncation logic:
public interface ITruncator
{
string Truncate(string value, int length, string truncationString, TruncateFrom truncateFrom = TruncateFrom.Right);
}
Example custom truncator:
public class SentenceTruncator : ITruncator
{
public string Truncate(string value, int length, string truncationString, TruncateFrom truncateFrom)
{
// Custom logic to truncate at sentence boundaries
// Implementation details...
}
}
// Usage
"First sentence. Second sentence. Third sentence."
.Truncate(30, new SentenceTruncator());
Real-World Examples
Truncate Article Previews
var article = "This is a very long article that needs to be truncated for the preview.";
var preview = article.Truncate(50, Truncator.FixedNumberOfWords);
// => "This is a very long article that needs to be…"
Truncate File Names
var fileName = "Very_Long_File_Name_That_Needs_Truncation.pdf";
var displayName = fileName.Truncate(20, "…", Truncator.DynamicLengthAndPreserveWords);
// => "Very_Long_File_Name…"
Truncate User Comments
var comment = "This is a user comment that might be too long to display in a notification.";
var notification = comment.Truncate(40);
// => "This is a user comment that might be…"
Best Practices
-
Choose the right strategy:
- Use
FixedLengthfor consistent visual length - Use
FixedNumberOfWordswhen word boundaries matter - Use dynamic strategies when readability is more important than exact length
- Use
-
Consider the truncation indicator:
- Use
…(single character) for space efficiency - Use
"..."if your audience expects it - Use
" [more]"or similar for clarity
- Use
-
Test with your actual content: Different text may truncate differently depending on word boundaries